Online courses in ASP.NET MVC, ASP.NET Core, and Design Patterns conducted by Bipin Joshi. Read more...
Learn ASP.NET MVC, ASP.NET Core, and Design Patterns through our online training programs. Courses conducted by Bipin Joshi on weekends. Read more details here.

<%@ Page %>

Introduction to ASP.NET Validation controls

Introduction

ASP.NET provides a set of web controls called validation controls that are used to validate HTML forms. This article discusses various types of validation controls available with code samples.

What are validation controls?

Most of the times while using HTML forms we need to validate the user input for certain criteria. In ASP there was no other way but to write our own server or client side validation routines. ASP+ frees the programmer from performing common validation tasks through server side validation controls.

There are 6 types of validation controls available :

  • RequiredFieldValidator

  • CompareValidator

  • RangeValidator

  • RegularExpressionValidator

  • CustomValidator

  • ValidationSummary

One HTML or web control can have more than one validations attached to it.

Following sections illustrates how to perform most common validation tasks using these controls

Using RequiredFieldValidator control

As the name suggests this control is used to validate fields that requires user input i.e. the fields which are mandatory.

Consider  following code snippet from a ASP+ page:

<form id="form1" runat="server">
<asp:textbox id="text1" runat="server" text="text1" />
<asp:button id="button1" runat="server" autopostback="true" />
<asp:requiredfieldvalidator id="valid1" runat="server"
controltovalidate="text1" errormessage= "Field is mandatory" /><BR></form>

If you run the page and try to submit the form with empty text box then you will see the message provided in errorMessage property of the validation control displayed next to the text box.

Note : the location of error message depends on where you have placed the validation control in the page. It need not be immediately following the control to be validated

Using CompareValidator control

This control is used to compare value of a control with :

  • value from another control or

  • a specific value

Typical use of this control can be while validation old and new passwords.

Consider following code which illustrates the first case:

<form id="form1" runat="server">
<asp:textbox id="text1" runat="server" text="text1" />
<asp:textbox id="text2" runat="server" text="text2" />
<asp:button id="button1" runat="server" autopostback="true" />
<asp:comparevalidator id="valid1" runat="server"
controltovalidate= "text1"controltocompare="text2" 
errormessage= "Field comparisn failed" /><BR></form>

Consider following code which illustrates the second case:

<form id="form1" runat="server">
<asp:textbox id="text1" runat="server" text="text1" />
<asp:textbox id="text2" runat="server" text="text2" />
<asp:button id="button1" runat="server" autopostback="true" />
<asp:comparevalidator id="valid1" runat="server"
controltovalidate="text1" valuetocompare= "valueto compare" 
errormessage= "Field comparisn failed" /><BR></form>

Whenever the text from both the controls differ, you will see the error message supplied.

Using RangeValidator control

Rangevalidator is used to ensure that the value of a control lies between the given range

<form id="form1" runat="server">
<asp:textbox id="text1" runat="server" text="text1" />
<asp:textbox id="text2" runat="server" text="text2" />
<asp:button id="button1" runat="server" autopostback="true" />
<asp:rangevalidator id="valid1" runat="server"
controltovalidate= "text1"minimumvalue="10" maximumvalue="100"
errormessage= "Valueout of range" /><BR></form>

Using CustomValidator control

Even though above controls provide great deal of functionality, you may need some customized validation not covered by in-built controls. Customvalidator control allows such facility. Here, you specify a function which will be called by the customvalidator control in order to determine weather the value entered in a control is valid or not. The function must return true or false. The signature of the function must match following syntax (in VB):

public function validate_function
(obj as object,str as string) as boolean

Consider following code :

<script language="vb" runat="server">

public function validate(obj as object, str as string) as boolean
        if text2.text="aaa" then
        return true
        else
        return false
        end if
end function

</script>


<form id="form1" runat="server" >
<asp:textbox id="text1" runat="server" text="text1" />
<asp:textbox id="text2" runat="server" text="text2" />
<asp:button id="button1" runat="server" text="Click" autopostback="true"/>
<asp:customvalidator id="valid1" runat="server" controltovalidate="text2" onservervalidationfunction="validate" errormessage="custom validation failed" />
</form>

Whenever the form is submitted the validation control invokes server side function validate() which checks the value of text2 text box and returns true or false on certain condition. If the function returns false then errormessage is displayed as usual.

Using ValidationSummary control

Validation summary control provides a nice way to display a list of all the validation errors occurred in the form. The list can be presented as bulleted list or paragraph or continuous text. This control do not perform validation of its own.

Consider following code :

<script language="vb" runat="server">

public function validate1(obj as object,str as string) as boolean
        if text2.text="aaa" then
        return true
        else
        return false
        end if
end function

</script>

<form id="form1" runat="server" >
<asp:textbox id="text1" runat="server" text="text1" />
<asp:textbox id="text2" runat="server" text="text2" />
<asp:button id="button1" runat="server" text="Click" autopostback="true"/>
<asp:requiredfieldvalidator id="valid1" runat="server" controltovalidate="text1" errormessage="field required" display="dynamic"/>
<asp:comparevalidator id="valid2" runat="server" controltovalidate="text1" controltocompare="text2" TYPE="STRING" errormessage="compare failed" display="dynamic"/>
<asp:comparevalidator id="valid3" runat="server" controltovalidate="text1" valuetocompare="10" TYPE="integer" errormessage="value compare failed" display="dynamic"/>
<asp:rangevalidator id="valid4" runat="server" controltovalidate="text1" minimumvalue="10" maximumvalue="100" type="integer" errormessage="range failed" />
<asp:customvalidator id="valid5" runat="server" controltovalidate="text2" onservervalidationfunction="validate1" errormessage="custom validation failed" />
<asp:validationsummary id="valid6" runat="server" headertext="Following errors occurred" displaymode="bulletlist" />
</form>

Here, the headertext specifies title of the summary list and display mode determines how the summary is displayed i.e. bulleted list, paragraph etc.

Checking validation status at page level

You can also check weather all the validations were successful or not using Page.IsValid property. The property returns true if there are no validation errors else returns false.

e.g

if page.IsValid then
'do some action
else
'display some message
end if



Bipin Joshi is a software consultant, trainer, author and a yogi having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, AngularJS, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Ajapa Meditation to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 18 Dec 2000



Tags : ASP.NET Server Controls