How to check from onclick if RequiredFieldValidator failed?

  • Thread starter Thread starter Bogdan
  • Start date Start date
B

Bogdan

Hi,

I have a OnClientClick script assigned to a button. From the script, I'd
like to check if RequiredFieldValidator attached to a textbox on the page
has failed. The Page_IsValid works fine with - for example - regex
validator but it does not work with the required field validator.

Any suggestions will be appreciated.
Thanks,
Bogdan
 
Hi Bogdan,

Please try the code below to check if page is valid or not in client side
script.

<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function ConfirmSave()
{
if (Page_IsValid )
{
return confirm('want to continue?');
}
else
{
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="TextBox1"

ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="return ConfirmSave();" UseSubmitBehavior="False"/></div>
</form>
</body>

Regards,
Manish
 
Manish,

Thanks for the reply. Unfortunately this does not work for me. It actually
illustrates the problem that I've been having - i.e. Page_IsValid is true
even when the text box is empty. So, regardless of the content of the text
box I always get the 'want to continue?' message box.

Does this example behaves differently on your system? If yes, what browser
and asp version do you use?
I tested it in IE, FireFox, and Safari with asp 2.0 on the server.

Thanks again,
Bogdan
 
Hi Bogdan.
I have the same problem. I got around it by initializing Page_IsValid to
false on the page. It is maybe not the best and cleanest solution but as far
as I can see it works.
 
Back
Top