Articles → .NET → Validation Controls In Asp.Net
Validation Controls In Asp.Net
- RequiredFieldValidator
- RangeValidator
- CompareValidator
- RegularExpressionValidator
- ValidationSummary
- CustomValidator
Requiredfieldvalidator
Click to Enlarge
Property | Description |
---|
ID | Unique identifier for RequiredFieldValidator control. |
ControlToValidate | ID of the control whose value is checked by validation control |
ErrorMessage | What will be the error message displayed when the control is empty. |
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvName" ControlToValidate="txtName"
ErrorMessage ="Please select value"
runat="server"></asp:RequiredFieldValidator>
<asp:Button ID="btnClick" runat="server" Text="Click" />
Click to Enlarge
Rangevalidator
Click to Enlarge
Property | Description |
---|
Type | Specifies the data type of user input. |
MinimumValue | specifies the minimum value of user input |
MaximumValue | specifies the maximum value of user input |
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:RangeValidator
ID="rvName"
ControlToValidate="txtName"
Type=" Integer "
ErrorMessage ="Value out of range"
MinimumValue="0"
MaximumValue="5"
runat="server"></asp:RangeValidator>
<asp:Button ID="btnClick" runat="server" Text="Click" />
Click to Enlarge
Comparevalidator
Click to Enlarge
Property | Description |
---|
Type | Specifies the data type of user input. |
ControlToCompare | One of the 2 controls which need to be compared. |
ValueToCompare | It specifies the constant value to compare with. |
Operator | Specifies the comparison operator |
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtName2" runat="server"></asp:TextBox>
<asp:CompareValidator
ID="rvName"
ControlToValidate="txtName"
Type="Integer"
ControlToCompare="txtName2"
Operator="Equal"
ErrorMessage ="Values are not equal"
runat="server"></asp:CompareValidator>
<asp:Button ID="btnClick" runat="server" Text="Click" />
Click to Enlarge
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:CompareValidator
ID="rvName"
ControlToValidate="txtName"
Type="Integer"
ValueToCompare="5"
Operator="Equal"
ErrorMessage ="Values are not equal"
runat="server"></asp:CompareValidator>
<asp:Button ID="btnClick" runat="server" Text="Click" />
Click to Enlarge
Regularexpressionvalidator
Click to Enlarge
Property | Description |
---|
ValidationExpression | Specifies the regular expression |
Enter 10 digit mobile number
<asp:TextBox ID="txtMobileNumber" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator id="reMobileNumber"
ControlToValidate="txtMobileNumber"
ValidationExpression="\d{10}"
ErrorMessage="Mobile number must be 10 numeric digits"
runat="server"/>
<asp:Button ID="btnClick" runat="server" Text="Click" />
Click to Enlarge
Validationsummary
<div>
<asp:ValidationSummary ID="ValidationSummary1" runat="server"/>
<br />
Enter 10 digit mobile number
<asp:TextBox ID="txtMobileNumber" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="reMobileNumber" ControlToValidate="txtMobileNumber"
ValidationExpression="\d{10}" ErrorMessage="Mobile number must be 10 numeric digits"
runat="server" />
<asp:Button ID="btnClick" runat="server" Text="Click" />
</div>
Click to Enlarge
Customvalidator
Click to Enlarge
<asp:ValidationSummary ID="ValidationSummary1" runat="server"/>
<br />
Enter 10 digit mobile number
<asp:TextBox ID="txtMobileNumber" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvMobileNumber" OnServerValidate="cvMobileNumber_Validate"
ErrorMessage = "Please enter the mobile number" Display="None"
runat="server" />
<asp:Button ID="btnClick" runat="server" Text="Click" />
protected void cvMobileNumber_Validate(object sender, ServerValidateEventArgs e) {
if (string.IsNullOrWhiteSpace(txtMobileNumber.Text)) {
e.IsValid = false;
}
}
Click to Enlarge
<asp:ValidationSummary ID="ValidationSummary1" EnableClientScript="true"
runat="server" />
<br />
Enter 10 digit mobile number
<asp:TextBox ID="txtMobileNumber" runat="server"></asp:TextBox>
<asp:CustomValidator ID="cvMobileNumber" ClientValidationFunction = "validate"
ErrorMessage = "Please enter the mobile number" Display="None"
runat="server" />
<asp:Button ID="btnClick" runat="server" Text="Click" />
<script type="text/javascript">
function validate(val, args) {
if (document.getElementById('<%=txtMobileNumber.ClientID %>').value == "") {
args.IsValid = false;
}
}
</script>