We help IT Professionals succeed at work.

VB/ASP.Net 2.0: Page.IsValid

k3n51mm asked
Last Modified: 2013-11-07
I'm making an ASP.Net registration form. It has username, password + confirmation, first name, last name, email, address info...all the things expected from a registration form.

I'm using client-side validators in the ASP form. They seem to be working ok. They're all required field validators, except for a couple regex for email address, phone number and zip code, and a compare validator to make sure the user selects a state from the dropdownlist. This is all pretty standard stuff.

However, when I check Page.IsValid, it always evaluates to False, even if all the form's fields are perfect. So, the data never gets stored.

For clarification, I'm using an <asp:button> to call a 'button_click(source as object, e as EventArgs)' sub, which is where I'm checking Page.IsValid from.

I'm a little rusty on ASP.Net. So what am I doing wrong?
Watch Question

The validate event does not occur until after Page Load. You are probably checking it in Page Load as is common. You can either wait and check it after the validators have fired, or, and this is how it is often done, raise the validation event yourself:
call Page.Validate() to fire all the validators. Then check Page.IsValid

You can also fire individual validators by raising their event:
if validator1.IsValid...
This one is on us!
(Get your first solution completely free - no credit card required)



I'm checking Page.IsValid in a Sub I call as the result of the OnClick event of the button. Here's its declaration in the body of the page:

<asp:button onClick="reg_click" id="reg1" text="Register" runat="server"/>

Sub RegClick(src as Source, e as EventArgs) includes this:

If Page.IsValid........

So, I'm not checking it in Page_Load. Looking at other stuff I did back in 2003, that IS where I'm doing it, using  "If Page.IsPostback". Returning to ASP after a few years, I saw no reason to involve Page.IsPostback in the process, so I'm explicitly calling Page.Validate in the button click event.



Not to my knowledge. I'm just collecting the text values from the boxes, storing them in SQL parameters, and calling the stored proc on the server to ExecuteNonQuery. This part of the process works fine. If I fill in all the fields, remove Page.IsValid from the process, and click 'Register', it all goes in smoothly. But, If I use Page.IsValid as summarized above, it always evaluates to False, even if the fields all appear to be correct.

If all fields are correctly filled in, then no, either before or after clicking the Register button, none of the validators are showing an error.

It seems to me the validators themselves are working okay - it's just that Page.IsValid refuses to return True for some reason.
This one is on us!
(Get your first solution completely free - no credit card required)


I can't set a breakpoint because I'm not using Visual Studio for this, just typing in a bit of code (so I thought).

But, I did as you asked: removed all validators, and added a response.Write of Page.IsValid....and it's true now.
What would my next step be?


Ok, so somewhere at least one of your validators is in fact setting false.
NazoUK had a pretty good idea of checking to see if you modified your form anywhere. To do that I suppose you would have to response.write them all out after page_load, or wherever you are calling your stored proc. But it would be easier to try first I think to response.write out all of the validators IsValid property:
validator1.IsValid.ToString(), etc

Are they all clientside validators, or do you have a custom validator in there somewhere? If so, thats the most likely to set false, so thats another to check. I know you said they were pretty routine stuff, but its an idea.
Im at work and heading home know, but I will check in later tonight.

If you still dont see any fields that would fail


Thanks for your help in thinking it through, I found the issue.

I just started adding the Validators back to the code one at a time until one failed.

I had a telephone number validator that required a certain pattern of numerals.... so I just
deleted the validator. As mentioned, this is a lightweight little task, so there's no mission-critical
validation going on.

I added the rest and they're all behaving, so all is well.

Thanks again, both of you.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.