Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

Implementing Validation Controls

Validation controls are used to validate the user's input. The validation controls can be attached to input controls to check the values that are entered by the user. You can use a validation control to perform tasks, such as checking whether a user has entered any value for the required filed, testing the entered value against a specific value or pattern of characters, and verifying that the entered value lies within a range.

For example, you can use validation controls to check whether a user has entered a password, whether the enterer password contains a minimum of four characters and a maximum of twenty characters, and whether the entered password is a combination of letters and numbers.

The six validation controls in ASP.NET are

  • RegularFieldValidator

  • RegularExpressionValidator

  • CompareValidator

  • RangeValidator

  • ValidationSummary

  • CustomValidator

RequiredFieldValidator

The RegularFieldValidator validation control is used to check whether a server control added to a Web form has a value or not. For example, you can use this validation control to check whether a user has entered any value in the password box.

The following lists some properties of the RegularFieldValidator:

PROPERTIES

DESCRIPTION

ControlToValidate

It is used to specify the ID of the control to be validated.

ErrorMessage

It is used to specify the error message displayed when the validation condition fails.

Text

It is used to specify the error message displayed by the control.

The commonly used method is Validate (). This method is used to perform validation on the control and setting the IsValid property. To use the RegularFieldValidator control:

  1. Open a new ASP.NET Web application.

  2. Add two label and two text boxes to the form.

  3. Add two RequiredFieldValidator controls

  4. Add a button control to the form.

  5. Set their properties as follows:

CONTROL TYPE

PROPERTIES

VALUE

TextBox

ID

txtname.

TextMode

Singleline

TextBox

ID

txtpassword

TextMode

Singleline.

RequiredFiledValidator

ID

RequiredFieldValidator1

ErrorMessage

Cannot leave the text box name empty.

ControlToValidate

txtname

RequiredFiledValidator

ID

RequiredFieldValidator2

ErrorMessage

Cannot leave the text box password empty.

ControlToValidate

txtpassword

Button

Text

Submit

 6. Press F5 to run.

 If you leave the text boxes blank the output will be like:

And if you leave one text box blank the output will be like

RegularExpressionValidator

It is used to check whether the server control added to the Web form matches with a specific regular expression or not. The regular expression can be the format of a telephone number or an e-mail address.

The following table lists the properties of RegularExpressionValidator.

PROPERTIES

DESCRIPTION

ControlToValidate

It is used to specify the ID of the control to be validated.

ErrorMessage

It is used to specify the error message displayed when the validation condition fails.

ValidationExpression

It is used to specify the regular expression, when performing validation.

The commonly used method is Validate (). This method is used to perform validation on the control and setting the IsValid property. To use the RegularExpressionValidator control:

  1. Open the same application used above.

  2. Add a label and textbox (id-txtemail) for e mail-ID

  3. Add a RegularExpressionValidator control on to the form and set its property as follows:

PROPERTY OF REGULAREXPRESSIONVALIDATOR

VALUE

ID

RegularExpressionValidator1

ControlToValidate

txtemail.

ErrorMessage

E-mail id is not valid

ValidationExpression

^[\w\.=-]+@[\w\.\-]+\.[a-z]{2,4}$

Note: How do we write in these validation expressions is discussed below in the next section.

Now if you enter incorrect format for the e mail id the output would be as shown below:

And for if you enter it in correct format no such error message would be displayed.

Regular Expressions

Regular expression is asequence of characters that represents a string. Regular expression is a technique used for pattern matching. The following table lists the commonly used valid expressions:

Expression for Checking

Syntax

Description

Example

A set of Characters

[ ]

Used to match any character within the [ ]. You can specify a range of characters by listing the starting and ending characters separeated by a dash (-), such as [a-z].

P[0-5], it is one which starts with P and followed by a digit within the range 0-5.

Word Characters

\w

Used to match any letter, numeric and underscore character.

\w{8,20}, it is like to validate a password with minimum length of 8 and maximum of 20 characters

Tagged Expression

{n}

Used to match an expression exactly n times.

P[0-5] {4}, to validate a product id that starts with the letter P followed by four digits ranging from 0-5

Any Character

 

Used to match any character except a line break.

12.25.02, to check for data formats such as 12/25/02, 12-25-02, 12 25 02 and even 12.25.02

Any non white space character

\S

Used to match any character except spaces, tabs, and line breaks.

http://\S\S\S\S.\S\S\S, to check whether a valid Web address has been entered such as http://niit.com or http://abcd.net

One or more characters

+

Used to match at least one occurrence of the preceding expression.

\S+@\S+\.\S+, to validate any email id.

Any Single Character

?

Used to match any single preceding character in an expression.

programs?, to specify that letter s is optional

Any white-space character

\s

Used to match any character including space, tabs and line breaks.

\sip, to check the space before the word.

Any digit character

\d

Used to match any digit in the range 0 to 9.

\d{5}, to check for a zip code that has exactly five digits, such as 11001.

Escape Character

~

Used to match any character following backslash

\~, to check whether the character tilt (~) has been entered by a user.

Zero or more Characters

*

Used to match zero or more characters in an expression.

P*\d{3}, to check for the entry of a product id that has to start with the letter P, such as P555, P222.

CompareValidator

The ControlValidator control is used to compare the entered value with another value. The other value can be  a number or  a value entered into another control. The following table lists the properties of the CompareValidator control:

PROPERTIES

DESCRIPTION

ControlToCompare

It is used to specify the ID of the control that will be used to compare values.

ControlToValidate

Used to specify the ID of the control that is to be validated.

ErrorMessage

It is used to specify the error message displayed when the validation condition fails.

ValueToCompare

It is used to specify the ID of the control to be compared.

To test this open the same application and add a label and  a text box (Cpassword). Add the CompareValidator control and set its properties as follows:

PROPERTY OF COMPAREVALIDATOR

VALUE

ID

CompareValidator1

ControlToValidate

Cpassword

ErrorMessage

Confirm Password does not match password

ControlToCompare

txtpassword

Press F5 to run. If your confirm password is not same as password, the output would be like:

RangeValidator

The RangeValidator control is used to check whether the value of a particular Web form field is within the range of MinimumValue and the MaximumValue. These values can be dates, numbers, currency amounts, or strings.

PROPERTIES

DESCRIPTION

ControlToValidate

Used to specify the ID of the control that is to be validated.

ErrorMessage

It is used to specify the error message displayed when the validation condition fails.

MaximumValue

Used to specify the maximum value

MinimumValue

Used to specify the minimum value.

To test this control, again open the same application and add a text box (txtage), a label and a RangeValidator  control. Change the properties of the rangevalidator as shown in the table below:

PROPERTY OF RANGEVALIDATOR

VALUE

ID

RangeValidator1

ControlToValidate

txtage

ErrorMessage

Age should be between 18-60

MinimumValue

18

MaximumValue

60

Now build the application and run it. If you enter a value that doesn't fall into valid range, the error message would be displayed.

ValidationSummary

The ValidationSummary control is used to summarize all errors and display the error list in a used-specified location on the Web page. The following table lists the properties of the ValidationSummary control:

PROPERTIES

DESCRIPTION

HeadText

Used to set the text displayed at the top of the summary.

ShowMessageBox

It is used to display the error message in  a pop-up message box when the value of this property is true.

ShowSummary

Used to enable or disable the summary of error messages.

To use this control:

Open the application developed in the preceding example. Add the ValidationSummary control to the form.

PROPERTY OF VALIDATIONSUMMARY

VALUE

ID

ValidationSummary1

HeaderText

Correct the following error before submitting the page.

ShowMessageBox

True

ShowSummary

False

Now run the application, if your application has errors they would be displayed in a message box like this as shown below:

CustomValidator

The CustomValidator control is used to perform user-defined validations that cannot be performed by standard validation controls.The following tables lists the properties of the CustomValidator control:

PROPERTIES

DESCRIPTION

ControlToValidate

Used to specify the ID of the control that is to be validated.

ErrorMessage

It is used to specify the error message displayed when the validation condition fails.

 The following tables lists the methods of the CustomValidator control:

METHODS

DESCRIPTION

OnServerValidate

Used to raise the ServerValidate event.

Validate

Used to perform validation and set the Isvalid property

ServerValidate is the commonly used event of CustomValidator control. You can write an event handler for the ServerValidate event and perform the required validation.

To understand the Validation Controls better, I am attaching the code discussed above. The project will also cover practical demonstration of CustomControls, which is a bit different than rest of the controls.

The validation required in this problem is, if marks obtained are greater than 75 or not.


Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |