January 5, 2011

Required Field Validator on Lost Focus

One of my students asked this question

I have 2 validation controls for the Name field. Required Field Validator and Regular Expression Validator.
If I don't enter any Name and move to the next field , the required field Validator doesn't give the error message.
If I enter wrong values,
Regular Expression Validator gives the error message. Then if I empty the name field and move to the next field , then Required Field Validator gives the error message.

My question is why the
Required Field Validator doesn't give the error message the first time I go to the next field without entering the values ...

Answer
The ASP.NET validation doesn't fire validation when press tab unless a textual change was made. You still get validation when the user clicks the button. It will find all the blank textboxes that are required and will validate them.


So If you want to fire the requiredfieldvalidation for
TextBox1
on onBlur() ,write
below code in pageload event of the webform
TextBox1.Attributes.Add("onblur", "ValidatorOnChange(event);");

It will validate the
TextBox1
on tab out ..

No comments:

Post a Comment