I have an ASP.NET web app. I am basically tryign to say a user has to enter eitehr the company name or the owner's name. I have my web page, and on the web page is a user control with those text fields and a custom control. Here is the custom control and validation summary...
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
CssClass="failureNotification" ValidationGroup="OwnerInfo" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ClientValidationFunction="CheckOwner" ControlToValidate="txtCompany"
ErrorMessage="Company Name or Owner required" ValidationGroup="OwnerInfo">*</asp:CustomValidator>
Even stripping the actual javascript function down to the bare minimum and trying it in both the control itself and the Headcontent section of the main page, I can't get it to fire. Here is the javascript function...
<script type="text/javascript">
function CheckOwner(source, args) {
args.IsValid = false;
}
</script>
Try this
<script type="text/javascript">
function CheckOwner(source, args) {
return false;
}
</script>
Are you trying to get it to fire for empty text? Is so you need to set the ValidateEmptyText property.
Related
In the application I'm developing, I using both the Validation controls provided by ASP.NET Web Forms and for certain parts of the form I'm using JavaScript.
Both aspects of the validation works as it should, however, when I click on the Submit button the ASP.NET validation comes first, then follows my custom validation made in JavaScript. As a result, when the form is incomplete and submitted, only the ASP.NET validation errors show, and when those errors are corrected and the form is submitted, then my validation errors are shown.
What I want is that when a user clicks on the Submit button, both the ASP.NET validation and my JavaScript validation occur simultaneously so that users can see all the errors at once.
<form id="StudentLSFApplication" runat="server" onsubmit="return validateForm()">
<!--other html code-->
<asp:Button ID="SaveLSFApplication" runat="server" Text="Save Application" OnClick="saveApplication"
ValidationGroup="AllValidation" />
</form>
<script>
function validateForm() {
//my validation
}
</script>
As you can see from my sample code, I have the JavaScript function validateForm() being called in an onsubmit event, and Button control is part of the AllValidation validation group. At the moment, on form submit the AllValidation occurs first, and when that is okay, then validateForm() occurs.
I think what you're looking for is the CustomValidator. You can use it like this:
<script type="text/javascript">
function validateFormElement(sender, args) {
if (args.Value == "waarde") {
args.IsValid = true;
} else {
args.IsValid = false;
}
}
</script>
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="myValidation"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Input error" ValidationGroup="myValidation" ClientValidationFunction="validateFormElement" ControlToValidate="TextBox1"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="myValidation" />
And you can still use all the other validators. No need to cram every validation you want into that one javascript function.
Required field validation is normally done on button clicks. But I want the alert to be shown when we go to the next TAB index. How can I do that in JavaScript---ASP.NET
Try this:
Script:
<script type="text/javascript">
function validate(c)
{
if(c.value=="")
{
alert("The required field cannot left blank");
c.focus();
}
}
</script>
Design:
<asp:TextBox ID="TextBox1" runat="server" onblur="validate(this)"></asp:TextBox>
Try using javascript for this for example as mentioned in this link
http://forums.asp.net/t/1376939.aspx
In Page_Load,
TextBox1.Attributes.Add("onblur", "ValidatorOnChange(event);")
Im trying to redirect a user to a new page in which there username will be displayed within a text box. I am using ASP.NET Loginname and Buttons to do this. My issue is that im not 100% sure on how to get the value from the LoginName into the javascript which needs this. I have no way to get into the server code so this all has to be done by SharePoint designer
I know the common way is to do something like this
window.location="http://mysite/default.aspx?u="+ LoginName1
But this seems it doesn't want to work. Any answers?
JavaScript Code
function Redirect()
{
window.location="http://mysite/default.aspx";
}
ASP.NET Code
<asp:Button runat="server" Text="To Sysomos" id="Button1" OnClientClick="if (!Redirect()) { return false;};"></asp:Button>
<asp:LoginName runat="server" id="LoginName1" ></asp:LoginName>
try something like this
<script>
var usrName = "#HttpContext.Current.User.Identity.Name";
</script>
window.location="http://mysite/default.aspx?u='+ usrName + '";
Reference :
How to get the current login user name in my Script file inside my asp.net mvc
Assuming you are using SP2010, you can use Sharepoint's Client Object Model to get the current user details.
<script type="text/javascript">
(function () {
var spUser;
// Ensure SP objects have been loaded before executing our code
ExecuteOrDelayUntilScriptLoaded(getSpUser,"sp.js");
function getSpUser() {
var clientContext = new SP.ClientContext.get_current();
var spWeb = clientContext.get_web();
spUser = spWeb.get_currentUser();
clientContext.load(spUser);
clientContext.executeQueryAsync(getSpUserSuccess, getSpUserException);
}
function getSpUserSuccess(sender, args) {
var curUserId = spUser.get_loginName();
// redirectToSomeAspxPage();
}
function getSpUserException(sender, args) {
// Do any necessary error handling.
}
})();
</script>
Only issue with this is that the time to redirect may take (slightly) longer as the code needs to wait for sp.js to load before you can get the current user's details. Alternatively, you can redirect without the username on the query string, and simply retrieve the username from your ASPX page using the above code.
You can find the username and other things for the current user. To do it, start by adding this line at the top of your .aspx page: <%# Register Tagprefix="SPSWC" Namespace="Microsoft.SharePoint.Portal.WebControls" Assembly="Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
(Version=14.0.0.0 for Sharepoint 2010 and Version=12.0.0.0 for Sharepoint 2007)
Now, just after the <form> tag add this line:
<form>
<SPSWC:ProfilePropertyLoader runat="server"/>
Finally add the below block before the </form> tag:
<div id="userDetails" style="display:none">
<asp:LoginName runat="server" id="userLogin">
<SPSWC:ProfilePropertyValue PropertyName="FirstName" ApplyFormatting="false" id="userFirstName" runat="server"/>
<SPSWC:ProfilePropertyValue PropertyName="LastName" ApplyFormatting="false" id="userLastName" runat="server"/>
<SPSWC:ProfilePropertyValue PropertyName="WorkEmail" ApplyFormatting="false" id="userWorkEmail" runat="server"/>
<SPSWC:ProfilePropertyValue PropertyName="PreferredName" ApplyFormatting="false" id="userPreferredName" runat="server"/>
</div>
So in your page you'll see a "userDetails" block with the current user login, firstname, lastname, work email and preferred name. With JavaScript you can get the username with :
document.getElementById('ctl00_userLogin').innerHTML
Currently in my application I am having a button and a text box. The user types something in the textbox and then presses the button. What I want is that:
The search button should should stay disabled when the page loads for the first time. I can achieve that by setting it to disabled in the code behind file.
Now I want it to remain disabled when the user types upto 2 characters. The moment the user types third character the button should get enabled automatically.
The important thing is that it has to be done without asp .net AJAX since this website will be run from old mobile phones. So only pretty basic javascript or jquery is supported.
Any help will be appreciated.
Thanks
Varun
in order to use the document.getElementById in asp.net and not have to use the full name, you should let asp.net provide it. Instead of:
document.getElementById("ctl00_ctl00_phContent_phPageContent_btnSearch")
Try:
document.getElementById('<%= btnName.ClientID %>')
Where btnName is the asp:Button Id. The <%= code will generate the actual button id, fully qualified, so you don't have to worry about things changing with the hard coded id.
I got this to work with a HTML text box, I don't think you can do it with a asp.net text box:
<head>
<script type="text/javascript">
function TextChange() {
var t = document.getElementById("Text1");
var b = document.getElementById("Button1");
if (t.value.length > 2) {
b.disabled = false;
}
else {
b.disabled = true;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Text1" type="text" onkeyup="TextChange();" />
<asp:Button ID="Button1" runat="server" Text="Button" Enabled="False" />
</div>
</form>
</body>
If you're using jQuery, use
$(<selector>).val().length
to get the size, then you can set the button's disabled attribute with
$(<button selector>).attr('disabled',false).
Let say there is a RequiredFieldValidator and when validation happens I want to make some changes on the page (like show a hidden div) if validation fails,
Is there a way to call a function in js when the validation ends?! or do you have any other idea?!
Thanks,
Adler!
Why not write your own validator that extends RequiredFieldValidator?
This tutorial looks like it could help point you in the right direction:
Or you can write custom client side java validator:
<script type="text/javascript" language="javascript">
function ValidateDropDown(source, arguments) {
if (arguments.Value === 'NA') {
arguments.IsValid = false;
}
}
</script>
<asp:CustomValidator ID="CoverServerCustomValidator" runat="server" ErrorMessage="<img src='../Themes/WebOrder/Images/weborder_Error.png'>"ToolTip="A Size is Required" Display="Dynamic" CssClass="Error" ValidationGroup="Sizing" OnServerValidate="ValidateDropDown" ControlToValidate="ddlCover" EnableClientScript="true"></asp:CustomValidator>