Form Validation in Asp.Net.
Thank You. Like, Share and Subscribe 👍 Followlect.com
The Form validation control are used to control user to enter valid data. It also give warning to user for invalid data entry. The validation control validate data entry in client side. It prevent user to store invalid, inappropriate and inconsistency data in database.
ASP.NET Validation Types.
- RequiredFieldValidator
- RangeValidator
- CompareValidator
- RegularExpressionValidator
- CustomValidator
- ValidationSummary
RequiredFieldValidator
Normally it is used along with the textbox or user input control. It make sure that the textbox is not empty. The user must enter value to textbox otherwise it warn user to enter value to textbox. for example
<asp:TextBox ID="txt_name" runat ="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv_name" runat ="server" ControlToValidate ="txt_name" ForeColor ="red" ErrorMessage ="Enter Your Name" ></asp:RequiredFieldValidator>
<asp:DropDownList runat ="server" ID="lb_gender">
<asp:ListItem Value ="0" >Select</asp:ListItem>
<asp:ListItem >Male</asp:ListItem>
<asp:ListItem >Female</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfv_gender" InitialValue ="0" runat ="server" ForeColor ="red" ControlToValidate ="lb_gender" ErrorMessage ="Select Gender"></asp:RequiredFieldValidator>
The RequiredFieldValidator is placed immediately below textbox. The ControlToValidate attribute is set with textbox id (ControlToValidate ="txt_name), attribute ErrorMessage is warn message to user.
RangeValidator
The RangeValidator is used check whether the user value is between the specified range.
<asp:TextBox ID="txt_age" runat ="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv_age" runat ="server" ForeColor ="red" ControlToValidate ="txt_age" ErrorMessage ="Enter Age" ></asp:RequiredFieldValidator>
<asp:RangeValidator ID="rv_age" runat ="server" ControlToValidate ="txt_age" MinimumValue ="17" MaximumValue ="24" ErrorMessage ="Enter Valid Age"></asp:RangeValidator>
In RangeValidator have the following attributes
- MinimumValue specifies minimum value in range
- MaximumValue Specifies the maximum value in range
- The Type attribute defines the type of value Date, Integer, Double, Currency and String.
CompareValidator
It is used to compare user value with another control value or constant value.
<asp:CompareValidator ID="CompV1" runat="server" ErrorMessage="Enter Correct Value">
In CompareValidator have the following attributes.
- Type defines the type of value.
- ControlToCompare specifies the comparing component control id.
- ValueToCompare specifies the constant value to compare.
- Operator specifies the any Relational Operator or DataTypeCheck
The above code to compare password and confirm password.
<asp:TextBox runat="server" id="Password" />
<asp:TextBox runat="server" id="ConfirmPassword" />
<asp:CompareValidator runat="server" id="cmpvalidator" controltovalidate="ConfirmPassword" controltocompare="Password" operator="Equal" type="String" errormessage="Confirm password must be equal." />
To compare constant value:
<asp:TextBox runat="server" id="txt1" />
<asp:CompareValidator runat="server" id="cmpvalidator" controltovalidate="txt1" ValueToCompare="21" operator="GreaterThan" type="Integer" errormessage="The value must be greater than 21" />.
RegularExpressionValidator
The RegularExpression is used check user value with specific pattern for example Emailid , telephone number.
<asp:TextBox ID="txtmail" runat="server" />
<asp:RegularExpressionValidator ID="RExpV" runat="server" ControlToValidate="txtmail"
ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Display = "Dynamic" ErrorMessage = "Invalid email address"/>
- ValidationExpression defines the type of pattern to match. The above will valid for EmailId.
- ControlToValidate specifies the id of control to validate
CustomValidation
In CustomValidation give option for developer to write custom validation code. The custom code can be Client Side validation or Server Side validation For Client Side use JavaScript or Vb-script and Server Side code from Asp code behind page VB.Net or C#.
For Server Side: onservervalidate attribute is set with code behind function.
<asp:TextBox runat="server" id="txtcv />
<asp:CustomValidator runat="server" id="cusval" controltovalidate="txtcv onservervalidate="cusval_ServerValidate" errormessage="Error Mesage" />
For Client Side: ClientValidationFunction attribute is set with JavaScript or Vb-Script.
<asp:CustomValidator runat="server" id="cusval" controltovalidate="txtcv ClientValidationFunction="cusvalfunc" errormessage="Error Mesage" />
ValidationSummary
It show overall summary of validation in form. The ValidationSummary show error message of each validation control that was failed in validation. This control is placed at the end of form before submit. button.
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red"/>
Comments
Post a Comment