Articles → .NET → Validation controls in asp.net

Validation controls in asp.net






















RequiredFieldValidator







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





Click to Enlarge


RangeValidator







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





Click to Enlarge


CompareValidator







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





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

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





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>





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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250