Validations in Asp.Net

Bangalore: Validation in a web form is  the process of checking the input entered by the end user is accurate or make sure whether the user has filled all required values in that form.
Asp.net provides different controls to validate your form so we can just go through it.
1) Required Field Validator
Its a validation control provided by the Asp.net which make the field as a mandatory one. with out giving input to the corresponding field it wont allow the user to submit the form.

syntax:
   <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>*
    <asp:RequiredFieldValidator id="fname" runat="server" ErrorMessage="Please Fill First Name" ControlToValidate="txtFirstName"></asp:RequiredFieldValidator>


here we are validating the text box First Name and using RFvalidatior we have made it as a mandatory one.

2) CompareValidator
This validator helps to compare two fields for example if you want to compare both password and confirm password

syntax:

 <asp:CompareValidator id="cnfpwd" ControlToCompare="txtPassword" ControlToValidate="txtConfirmpwd" runat="server" ErrorMessage="passwords should match" ></asp:CompareValidator>

ControlToCompare First Text box id
ControlToValidate Second text box id

3) RegularExpressionValidator 
If you want to validate the value inside a particular field then you have to use regular ex validation.
eg 1: Email Validation in Asp.net
 Syntax:

<asp:RegularExpressionValidator ID="unamss" runat="server" ErrorMessage="Use Email format" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="txtemil"></asp:RegularExpressionValidator> 

 eg 2: Allow only Integers in a Textbox

<asp:RegularExpressionValidator ID="unamss" runat="server" ErrorMessage="Use Only Integers" ValidationExpression="^\d+$"  ControlToValidate="txtemil"></asp:RegularExpressionValidator> 

In place of  ValidationExpression you can mention the way you want to validate that field

 4) Validation Summary 
In Validation Summary it helps you to show all error message in a summarized format, you can show it as a pop up message or in top of the form as per your requirement.

Syntax:
<asp:ValidationSummary ID="validationsumm" runat="server" DisplayMode="BulletList" EnableClientScript="true" ShowSummary="false" ShowMessageBox="true" />

5) Range Validator
Using Range Validator you can validate the min and max values of that validating field
 

<asp:RangeValidator ControlToValidate="txtrange" MinimumValue="1" MaximumValue="100" Type="Integer" EnableClientScript="false" Text="The value between 1 and 100" runat="server" > </asp:RangeValidator> 

6) Custom Validation 
Using custom validation you can write your own custom validation conditions

Syntax:
<asp:CustomValidator runat="server" id="csvalidation" controltovalidate="txtcval" onservervalidate="csvalidation_Validation" errormessage="text must 10 character length" />

In code behind C# write the validation condition

 protected void csvalidation_Validation(object sender, ServerValidateEventArgs ex)
        {
            if (ex.Value.Length == 10)
                ex.IsValid = true;
            else
                ex.IsValid = false;
        }

/* androidsharp */

0 comments: