How to check from onclick if RequiredFieldValidator failed?

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
 
M

Manish

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
 
B

Bogdan

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
 
P

Piglet

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.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top