Welcome to AspAdvice Sign in | Join | Help

ASP.NET - Javascript and Validator

when you need to call a javascript with a validator together, somehow the validator get passed by and the page get a refresh before the validator's message shows up. to cater for this look at the example below where i include the Page_ClientValidate() checking in the javascript

<script type="text/javascript">

function Validation()

{

var tempString = document.getElementById('<%=TextBox1.ClientID %>').value;

"text/javascript">

function Validation()

{

var tempString = document.getElementById('<%=TextBox1.ClientID %>').value;

function Validation()

{

var tempString = document.getElementById('<%=TextBox1.ClientID %>').value;

var tempString = document.getElementById('<%=TextBox1.ClientID %>').value;

if (tempString != "a" & Page_ClientValidate())

(tempString != "a" & Page_ClientValidate())

{alert('a')

return false;

}

else {

return true;

}

}

'a')

return false;

}

else {

return true;

}

}

return false;

}

else {

return true;

}

}

else {

return true;

}

}

return true;

}

}

</script>

script>

<asp:TextBox ID="txtUserId" runat="server"></asp:TextBox>

asp:TextBox ID="txtUserId" runat="server"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator" ControlToValidate="txtUserId" runat="server" ErrorMessage="Please enter a user Id" Display="Dynamic" />

<asp:RequiredFieldValidator ID="RequiredFieldValidator" ControlToValidate="txtUserId" runat="server" ErrorMessage="Please enter a user Id" Display="Dynamic" />

<asp:Button ID="Button2" runat="server" OnClientClick="return Validation()" Text="Button" />

<asp:Button ID="Button2" runat="server" OnClientClick="return Validation()" Text="Button" />
Sponsor
Published Wednesday, May 13, 2009 2:24 PM by mo meng

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below