This blog talks about the validation control in ASP.NET.
A validation server control is used to validate the data of an input control. If the data does not pass validation. It will display an error message to the control.
Types of validation
1. Client side validation
2. Sever side validation
Client side validation
Validation done in the browser before sending the form input data to the server using java script, jquery and vbscript is called client side validation.
Server side validation
Validation done at the server level after sending the form data to the server but before entering the data into the database is called server side validation. ASP.NET validation controls provides client –side validation and server side validation.
ASP.NET Validation Controls
RequiredField validator
Compare validator
Range validator
RegularExpression validator
CustomValidator
ValidationSummary
The RequiredFieldValidator control: The user does not skip a mandatory
entry field.
The CompareValidator control: Compares one controls value with another
controls value.
The RangValidator control: Checks the user’s input is in a given range.
The RegularExpressionValidation control: Checks that the user’s entry
matches a pattern define by a regular expression.
The CustomValidator control: Checks the user’s entry using custom-coded
validation logic.
The validationSummary Control: Displays a summary of all validation errors
inline on a web page, in a message box or both.
Example
//RequiredFieldValidator
<asp:RequiredFieldValidator ID="rfvUserID" runat="server" ErrorMessage="*" SetFocusOnError="True" ControlToValidate="txtUserID" ValidationGroup="grp"></asp:RequiredFieldValidator>
//RegularExpressionValidator
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
ControlToValidate="txtPinCode"
ValidationExpression="(D-)?\d{6}"
ErrorMessage="*"
runat="server" Display="Dynamic" ForeColor="Red" />
//CompareValidator
<asp: CompareValidator runat="server" id="cmpNumbers" controltovalidate="txtSmallNumber" controltocompare="txtBigNumber" operator="LessThan" type="Integer" errormessage="The first number should be smaller than the second number!" />
//RangeValidator
<asp:RangeValidator ID=”RangeValidator1” runat=”Server” ErrorMessage=”Enter age between 18 to 35” ControlToValidate=”txtAge” ForeColor=”Red” Maximum=”35” MinimumValue=”18” Type=”Integer”>
//CustomValidator
<asp:customValidator id=”customValidator1” forecolor=”Red” onservervalidate=”CustomeValidator1_ServerValidatr” errorMessage=”Password cannot be less than 5 character” controltovalidate=”txtPassword2” runat=”server”></asp:customvalidator>
Protected void customValidator1_serverValidator(object source, ServerValidateEventArgs args)
{
If(args.Value.Length<5)
args.IsValid=false;
Else
args.IsValid=true;
}
//ValidationSummaryValidation
<asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="Following error" DisplayMode="BulletList" ShowMessageBox="false" ShowSummary="true" />
You can also read these related post
https://www.mindstick.com/Articles/43/validation-controls-in-asp-dot-net
Leave Comment