Articles → .NET → Validation Controls In Asp.Net

Validation Controls In Asp.Net







  1. RequiredFieldValidator
  2. RangeValidator
  3. CompareValidator
  4. RegularExpressionValidator
  5. ValidationSummary
  6. CustomValidator



Requiredfieldvalidator






Picture showing the syntax of requiredfieldvalidator validation control in asp.net
Click to Enlarge



PropertyDescription
IDUnique identifier for RequiredFieldValidator control.
ControlToValidateID of the control whose value is checked by validation control
ErrorMessageWhat 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" />




Picture showing the output of requiredfieldvalidator validation control in asp.net
Click to Enlarge


Rangevalidator






Picture showing the syntax of rangevalidator in asp.net
Click to Enlarge



PropertyDescription
TypeSpecifies the data type of user input.
MinimumValuespecifies the minimum value of user input
MaximumValuespecifies 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" />




Picture showing the output of rangevalidator in asp.net
Click to Enlarge


Comparevalidator






Picture showing the syntax of comparevalidator in asp.net
Click to Enlarge

PropertyDescription
TypeSpecifies the data type of user input.
ControlToCompareOne of the 2 controls which need to be compared.
ValueToCompareIt specifies the constant value to compare with.
OperatorSpecifies 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" />




Picture showing the output of comparevalidator in asp.net
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" />


Picture showing the output of comparevalidator with ValueToCompare attribute in asp.net
Click to Enlarge


Regularexpressionvalidator






Picture showing the syntax of RegularExpressionValidator  in asp.net
Click to Enlarge

PropertyDescription
ValidationExpressionSpecifies 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" />




Picture showing the output of RegularExpressionValidator in asp.net
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>




Picture showing the output of validationsummary in asp.net
Click to Enlarge




Customvalidator






Picture showing the syntax of customvalidator in asp.net
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;
  }
}




Picture showing the output of customvalidator in asp.net
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>





Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, May 12, 2015

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250