Post contents :
- Introduction
- Validation Controls
- RequiredFieldValidator - verify the user will insert any input
- CompareValidator - compare vs a value
- RangeValidator - compare vs a range of values
- RegularExpressionValidator - check vs regular expression
- ValidationSummary - display all validation error in the page
- CustomeValidator - check vs your own validation method (full flexibility)
Introduction
Validation process checks that the user input (Text,Checked,..) is correct otherwise error is issued. E.g. in case a number should be entered : validation check that the user will not be able to insert a letter, or to be more accurate that this letter will not be sent to the server and an error will be issued.
Validation is performed in general on the client (preferred) in case it support JavaScript otherwise it is done on the server.
ASP.Net provides few controls to perform input validation.
Validation Controls
There are six validation controls in ASP.Net
- RequiredFieldValidator - verify the user will insert any input
- CompareValidator - compare vs a value
- RangeValidator - compare vs a range of values
- RegularExpressionValidator - check vs regular expression
- ValidationSummary - display all validation error in the page
- CustomeValidator - check vs your own validation method (full flexibility)
It is important to note that :
- Validation take place when user submit info to the server e.g. when a button is clicked or in any other way which cause POSTBACK (not on Page_Load).
- Page has a property IsValid which indicate if validation is ok for this page. It is based on the validation controls in this page. IsValid is false if at least one validator has IsValid=false otherwise it is true
- It is possible to use Response.Redirect("some url") in conjunction with Page.IsValid to indicate success validation
RequiredFieldValidator
Validate that a field is not empty
Important properties :
- ControlToValidate - id of control that should be validated
- ErrorMessage - text to display in ValidationSummary in case validation failed. This will appears for this control in case of error if Text property is empty
- Text - text to display in case validation failed
- EnableClientScript - whether to perform validation in client side if possible (default true)
- IsValid - whether validation is ok or not
- Display :
- None - no error display, only in ValidationSummary
- Static - space required for error is occupied anyway
- Dynamic - space required for error is occupied only when error occurs
Sample 1
RequiredFieldValidator - sample 1.zip
The sample is a web site with info the user has to fill :
- Name - required
- ID - required
- E-Mail - optional
- Cell Phone - optional
Error should be issued in case Name or ID are not entered
Redirect to "thank you" page in case validation is not ok.
View in browser and click on Submit :
Use breakpoint inside the Submit handler. When does the breakpoint is hit ? in case validation is ok or not ok ? why ? where is validation done in client or server ?
In case validation is OK use thank you page :
- use Page.IsValid (Validation will not be OK if at least one validator has failed.)
- use Response.Redirect("Url name")
CompareValidator
Validate the input against another value for a given operator
Important properties :
- ControlToCompare - compare this control with the value entered by the user. E.g. in case the input is entered viea TextBox you can compare the Text property with another TextBox which is the ControlToCompare.
- ValueToCompare - compare this value with the value entered by the user
- Operator :
- Equal
- GreaterThan
- GreaterThanEqual
- LessThan
- LessThanEqual
- NotEqual
- DataTypeCheck - validate type is correct (Type should also be defined)
- Same as in RequiredFieldValidator
- ControlToValidate
- ErrorMessage
- Text
- EnableClientScript
- Display
- validation will be ok in case the value to compare is empty , use also RequiredFieldValidator in this case (see )
The sample is a web site with info the user has to fill - guess day of birth.
Handle empty field (use RequiredFieldValidator, Display = dynamic)
Handle wrong value (use CompareValidator,Display = dynamic)
Handle correct value (use Page.IsValid and Response.Redirect)
Use breakpoint inside the "Guess my day of birth" handler. When does the breakpoint is hit ? in case validation is ok or not ok ? why ? where is validation done in client or server ?
RangeValidator
Validate the input against a range
Important properties :
- MinimumValue - allowed input minimal value
- MaximumValue - allowed input maximal value
- Type -
Specifies the data type of the value to check. The types are:
- Currency
- Date
- Double
- Integer
- String
- Same as in RequiredFieldValidator
Remark
- ControlToValidate
- ErrorMessage
- Text
- EnableClientScript
- Display
- validation will be ok in case the value to compare is empty , use also RequiredFieldValidator in this case (see )
RangeValidator_sample 3.zip
The sample is a web site with info the user has to fill - height.
Common height is defined between 120-220
RegularExpressionValidator
Validate that the input matches a regular expression
Important properties :
- ValidationExpression
- Same as in RequiredFieldValidator
- ControlToValidate
- ErrorMessage
- Text
- EnableClientScript
- Display
Remark
- validation will be ok in case the value to compare is empty , use also RequiredFieldValidator in this case (see )
You can choose via ValidationExpression regular expression from a list :
Sample 4
RegularExpressionValidator sample 4.zip
The sample is a web site with Email that need to be validated.
Non valid case
In case the mail is valid the page is redirected to result in :
Need to handle also empty field.
ValidationSummary
Display error messages from different validation controls (provided that their ErrorMessage property is not empty)
Important properties :
- ShowSummary - define whether the summary control should be visible or not
- ShowMessageBox - defines whether to show errors in message box
- DisplayMode :
CustomeValidator
Validate the user input against a custom method.
Important event
- ServerValidate - occurs when server validate e.g. when button is pressed
Important properties of ServerValidateEventArgs :
- Value - value to be validate
- IsValid - validation result
Important properties of CustomeValidator:
- Same as in RequiredFieldValidator
- ControlToValidate
- ErrorMessage
- Text
- EnableClientScript
- Display
Remark
- Validation will be ok in case the value to compare is empty , use also need RequiredFieldValidator in this case ( see)
- The handler of ServerValidate will NOT be invoked when another validation control like RequiredFieldValidator is used and it's IsValid is false. This is because the validation of RequiredFieldValidator is done on the client and when failed no info is sent to the server.
Double click on the control result in :
Sample 6
CustomeValidator_sample 6.zip
Build the following web site :
user name :
- length should be between 1-20
- only lower case letters are allowed
- issue an error in case rules are violated
password :
- length should be between 5-10
- only upper case letters are allowed
- issue an error in case rules are violated
Redirect to Login Success page in case validation is OK
Example for the case ServerValidate is not called because client validation has failed - User is empty is issued by RequiredFieldValidator which validate on the client
CauseValidation
CauseValidation_sample 7
It is possible to cause validation even if a button is not pressed.
This can be done by setting the following input control properties :
- CauseValidation = true
- AutoPostBack = true
Then by putting the mouse in the input (e.g. TextBox,RadioButton ...) and typing some thing (even space) then use Enter ot Tab will cause AutoPostBack and validation will be performed
Run to result :
Now put the mouse inside one of the TextBox and hit space, then click Enter or Tab, this cause POSTBAACK which cause validation which result in :
Home Project
Add the following validation to the home project of http://websoftwarenk.blogspot.co.il/2012/08/aspnet-web-form-using-server-control.html :
- Validate "Choose Starting Player"
- Validate "Choose Background"
- Text should be * and error should be displayed by summary validator
- Validate x Palyer \ 0 Player TextBox name :
- name is not empty
- name length is 5-20
- name contains only letters and digits
- Text should be * and error should be displayed by summary validator
Nathan


















































