IMG-LOGO

ASP.Net Input Validation Controls

andy - 07 Jul, 2013 8799 Views 0 Comment

In this tutorial you will learn how to use ASP.Net input validation controls like RequiredFieldValidator, CompareValidator, RegularExpressionValidator, CustomValidator, ValidationSummary, and RangeValidator. Those built in validation controls are really handy to help validation the form controls you going to create on your web pages.

1. ASP.Net RequiredFieldValidator Control

RequiredFieldValidator control is used to validate and check if an input text value is empty.

//Textbox to accept any input value
<asp:TextBox runat="server" ID="txtName" />

//required validator control to validate textbox input
<asp:RequiredFieldValidator ID="reqMyName" ControlToValidate="txtName" runat="server" ErrorMessage="Your name is required" Display="dynamic" />

2. ASP.Net CompareValidator Control

CompareValidator control is used to compare a value of one control to the other value in another control or other fixed value.

//Compare to Password field
<asp:TextBox runat="server" ID="txtPassword" TextMode="Password" />

//Compare Confirm Password field
<asp:TextBox runat="server" iD="txtConfirmPassword" TextMode="Password" />

//Compare Validator
<asp:CompareValidator ID="reqCompareValidator" runat="server" ControlToValidate="txtPassword" ControlToCompare="txtConfirmPassword" ErrorMessage="Both passwords have to be match." Display="dynamic" />

//Compare between one control to other fixed value
<asp:TextBox runat="server" ID="txtYearToJoin" />
<asp:CompareValidator ID="reqYearToJoin" runat="server" ControlToValidate="Age" ValueToCompare="17" Display="dynamic" ErrorMessage="Minimum requirement to join is 17 years old" Type="Integer" Operator="GreaterThanEqual" />

//Available operators types
Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, DataTypeCheck

3. ASP.Net RegularExpressionValidator Control

RegularExpressionValidator control is used to validate a control value by whether the control value is matched with given regular expression pattern.

//Textbox to accept numbers only
<asp:TextBox ID="txtNumber" runat="server" />

//RegularExpressionValidation Control
<asp:RegularExpressionValidator ID="regexNumber" runat="server" ControlToValidate="txtNumber" ValidationExpression="^[0-9] $" ErrorMessage="Invalid number" Display="Dynamic"/>

4. ASP.Net RangeValidator Control

RangeValidator control is used to validate if a control value matches with the predefined range value. Those predefined range values are MinimumValue, MaximumValue, and Type. There are 5 types that you can define, which will be Currency, Date, Double, Integer, and String.

//Textbox to accept numbers only between predefined range
<asp:TextBox runat="server" ID="txtNumber" />

//RangeValidator Control
<asp:RangeValidator ID="reqRangeValidator" runat="server" Display="dynamic" ControlToValidate="txtNumber" Type="Integer" ErrorMessage="The number must be between 100-200" MinimumValue="100" MaximumValue="200" />

5. ASP.Net CustomValidator Control

CustomValidator Control is used when you have a custom javascript validation or server behind code validation.

//Example of using javascript validation that will check if input entered falls between 100-200 integer value
<script type="text/javascript">
	function CheckNumberRange(source, arguments) {
		if (arguments.Value >= 100 && arguments.Value <= 200) {
			arguments.IsValid = true;
		} else {
			arguments.IsValid = false;
		}
	}
</script>

//Textbox field to accept number of integer range between 100 to 200
<asp:TextBox id="txtNumberOfRange" runat="server" />

//CustomValidator range

Another example that will use ServerValidation

//Textbox field to accept number of integer range between 100 to 200
<asp:TextBox id="txtNumberOfRange" runat="server" />

//Button to check the range value
<asp:Button id="btnCheckRange" Text="Validate" runat="server"/>
	
//Label to show the result
<asp:Label id="lblResult" runat="server" />

//CustomValidator range
<asp:CustomValidator id="reqCustomValidator" ControlToValidate="txtNumberOfRange"   OnServerValidate="CheckNumberRange" Display="Dynamic" ErrorMessage="Invalid range. Must be between 100-200" runat="server"/>

//Button validator to check and return the page validation
void btnCheckNumber_OnClick(object sender, EventArgs e){ 
	if(Page.IsValid){
		lblResult.Text = "Page is valid.";
	}else{
		lblResult.Text = "Page is not valid!";
	}
}

//Function Server Validator
void CheckNumberRange(object source, ServerValidateEventArgs arguments) {
	int num = 0;
	arguments.IsValid = false;
	if (int.TryParse(arguments.Value, out num)) {
		num = int.Parse(arguments.Value);
		if(num >= 100 && 200 >= num){
			arguments.IsValid = true;
		}
	}
}

6. ASP.Net ValidationSummary Control

ValidationSummary Control is used to show the list or summary of all available validator errors. It does not perform any validation.

//ValidationSummary Control
<asp:ValidationSummary ID="reqValidationSummary" runat="server" ShowSummary="true" ShowMessageBox="true" DisplayMode="BulletList" HeaderText="Please see the following errors:"/>

Variable Properties:

ShowMessageBox: true or false.

If set to true, it will display a message box.

ShowSummary: true or false.

If set to true, it will summary of the errors text in the page.

DisplayMode: BulletList, List, and SingleParagraph.

The style of the errors list text to be displayed.

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Blogs

Related Tutorials