A Validation control is used to validate the data of an input control. If the data does
not pass validation, it will display
an error message to the user.
There are six types of validation controls.
· RequiredFieldValidator
· RangeValidator
· RegularExpressionValidator
· CompareValidator
· CustomValidator
· ValidationSummary
RequiredFieldValidator
The RequiredFieldValidator control is used to make an input control a required
field.
This control will display error message if the field on which validation control is
applied is left blank.
Example
<formid="form1"runat="server">
<asp:TextBoxID="TextBox2"runat="server"
style="z-index: 1; left: 64px; top: 88px; position: absolute"TabIndex="3"></asp:TextBox>
<asp:TextBoxID="TextBox1"runat="server"
style="z-index: 1; left: 63px; top: 51px; position: absolute"TabIndex="1"></asp:TextBox>
<asp:ButtonID="Button1"runat="server"
style="z-index: 1; left: 68px; top: 122px; position: absolute"
Text="Button"/>
//applying RequiredFieldValidator to TextBox1
<asp:RequiredFieldValidatorID="RequiredFieldValidator1"runat="server"ControlToValidate="TextBox1"
ErrorMessage="* You must enter a value into textbox"
style="position:absolute; top: 51px; left: 220px;"Display="Static">*</asp:RequiredFieldValidator>
//applying RequiredFieldValidator to TextBox2
<asp:RequiredFieldValidatorID="RequiredFieldValidator2"runat="server"ControlToValidate="TextBox2"
ErrorMessage="* You must enter a value into textbox"
style="position:absolute; top: 87px; left: 222px;"Display="Static">*</asp:RequiredFieldValidator></form>
Screen shot
RangeValidator
The RangeValidator control is used to check that the user enters an input value that
falls between two values. It is possible to check ranges within numbers, dates, and
characters.
Example
<formid="form1"runat="server">
<asp:TextBoxID="TextBox1"runat="server"
style="z-index: 1; left: 60px; top: 55px; position: absolute"></asp:TextBox>
<asp:ButtonID="Button1"runat="server"
style="z-index: 1; left: 96px; top: 109px; position: absolute"Text="Button"/>
//applying range validator on TextBox1. Range should be between 20 to 50, otherwise it will display error message.
<asp:RangeValidatorID="RangeValidator1"runat="server"
ErrorMessage="* value should be between 20 to 50"
style="position:absolute; top: 159px; left: 60px; width: 209px;"
ControlToValidate="TextBox1"MaximumValue="50"MinimumValue="20"Type="Integer"></asp:RangeValidator>
</form>
Screen shot
RegularExpressionValidator
The RegularExpressionValidator control is used to ensure that an input value matches a specified pattern. Both server- and client-side validation are performed unless the browser does not support client-side validation or the EnableClientScript property is set to false.
This validation does not fail if field is left empty; in order to check for empty field we have to use RequiredFieldValidator control.
Example
<formid="form1"runat="server">
<asp:TextBoxID="TextBox1"runat="server"
style="z-index: 1; left: 101px; top: 45px; position: absolute; width: 197px;"></asp:TextBox>
<asp:LabelID="Label1"runat="server"Text="E-mail"
style="position:absolute; top: 48px; left: 32px; width: 92px;"></asp:Label>
//applying RegularExpressionValidator to TextBox1 to check for valid email address provided by the user.
<asp:RegularExpressionValidatorID="RegularExpressionValidator1"runat="server"ErrorMessage="Invalid Email ID"
style="position:absolute; top: 120px; left: 31px; width: 251px;"
ControlToValidate="TextBox1"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<asp:ButtonID="Button1"runat="server"
style="z-index: 1; left: 113px; top: 83px; position: absolute"Text="Button"/>
</form>
Screen shot
CompareValidator
The CompareValidator control is used to compare the value of one input control to
the value of another input control or to a fixed value.
This validator does not fail if the field is empty.
Example
<formid="form1"runat="server">
<asp:TextBoxID="TextBox2"runat="server"
style="z-index: 1; left: 30px; top: 100px; position: absolute"
TabIndex="3"></asp:TextBox>
<asp:TextBoxID="TextBox1"runat="server"
style="z-index: 1; left: 30px; top: 53px; position: absolute"TabIndex="1"></asp:TextBox>
<asp:ButtonID="Button1"runat="server"
style="z-index: 1; left: 58px; top: 149px; position: absolute"Text="Button"/>
//compare validator is applied to TextBox2, which will compare the value of TextBox2 with the value of TextBox1.
<asp:CompareValidatorID="CompareValidator1"runat="server"
ErrorMessage="Invalid Value"ControlToValidate="TextBox2"ControlToCompare="TextBox1"
style="position:absolute; top: 190px; left: 25px; width: 228px;"></asp:CompareValidator>
//compare validator is applied to TextBox1, value of TextBox1 is compared with 50 with LessThanEqual operator, so as to
//check the value in TextBox1 should not exceed 50
<asp:CompareValidatorID="CompareValidator2"runat="server"
ErrorMessage="Value should not be greater than 50"
style="position:absolute; top: 53px; left: 200px; width: 223px;"
ControlToValidate="TextBox1"Operator="LessThanEqual"ValueToCompare="50"
Type="Integer"></asp:CompareValidator>
</form>
Screen Shot
CustomValidator
CustomValidator control allows us to write method to handle validation of value entered.
Example
//code in .aspx file
<formid="form1"runat="server">
<asp:TextBoxID="TextBox1"runat="server"
style="z-index: 1; left: 114px; top: 48px; position: absolute"></asp:TextBox>
<asp:ButtonID="Button1"runat="server"onclick="Button1_Click"
style="z-index: 1; left: 149px; top: 95px; position: absolute"Text="Button"/>
//applying custom validation on TextBox1, and ‘CustomValidator1_ServerValidate’ method is used to validate input.
<asp:CustomValidatorID="CustomValidator1"runat="server"
ControlToValidate="TextBox1"ErrorMessage="Invalid Number" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator></form>
//code in .cs file
protectedvoid Button1_Click(object sender, EventArgs e)
{
//this will check for page validation.
if (Page.IsValid)
Response.Write("Valid Input");
}
//method which will be called when validation is required.
protectedvoid CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
//checking the value supplied by the user, to be between 20 to 50
if (int.Parse(args.Value) >= 20 && int.Parse(args.Value) <= 50)
//if value is true assigning true to IsValid property of arguments otherwise false.
args.IsValid = true;
else
args.IsValid = false;
}
Screen shot
ValidationSummary
The ValidationSummary control is used to display a summary of all validation errors occurred in a Web page. The error message displayed in this control is specified by the ErrorMessage property of each validation control. If the ErrorMessage property of the validation control is not set, no error message is displayed for that validation control.
You can also read these post
https://www.mindstick.com/Blog/768/validation-controls-in-asp-dot-net
Leave Comment
1 Comments
View All Comments