Execute client side script when validation fails - how?

T

turboJeeper

I have a form with 16 fields, 10 of which use the
RequiredFieldValidator controls. I want to display a custom message
when the validation fails. However, all the validators have
ClientScript enabled so there is no postback.

What can I hook into on the client side to execute other client side
script (eg. show a hidden label or popup) if validation fails?
 
G

Guest

Sure,

<asp:TextBox runat="server" ID="TextBox1" CausesValidation="false"/>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1"
ControlToValidate="TextBox1"
ErrorMessage="" Display="Dynamic" EnableClientScript="true"/>

<asp:TextBox runat="server" ID="TextBox2" CausesValidation="false"/>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator2"
ControlToValidate="TextBox2"
ErrorMessage="" Display="Dynamic" EnableClientScript="true"/>

<asp:TextBox runat="server" ID="TextBox3" CausesValidation="false"/>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator3"
ControlToValidate="TextBox3"
ErrorMessage="" Display="Dynamic" EnableClientScript="true"/>

<asp:Button runat="server" ID="Button3" Text="Click!" OnClientClick="return
ValidateUserData();"/>

<script type="text/javascript">
function ValidateUserData()
{
for (i = 0; i < Page_Validators.length; i++)
{
ValidatorValidate(Page_Validators);
}

ValidatorUpdateIsValid();

if (Page_IsValid == true)
{
return true;
}
else
{
alert('my custom code!');
return false;
}
}
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top