Friday 22 March 2013

example of all validators like EmailValidator,Mobile no validator etc... in asp.net



                 

What is RegularExpression:-

            RegularExpression is also known as Regxp.by using RegularExpression First you have set some property of the RegularExpression are as follow

  • Add a RegularExpressionValidator control to your page.
  • Set the ControlToValidate property to indicate which control to validate.
  • Set the ValidationExpression property to an appropriate regular expression.
  • Set the ErrorMessage property to define the message to display if the validation fails.
 

Description:-


in this example we explain all validation like email validation,phone number or Mobile number validation,URL validation,Zipcode,validation Password etc.......

we all know that the Validation is main part in every place when the Form data is submited.
For exaple:- suppose you enter your Name in Digit and your Mobile Number in String Format is not a proper way so for this Reason we can use Validation Before Our data is Submited to the Server.

 through validation we can also reduce the load in server because the your data validation is checked in Clientside and then after it is valid then and then submited to server for further process otherwise you show a validation message like enter mobile number 10 digit only or your Email id is not Valid.


  so through validation you can built your website very smoothly. 



Code:-



Email Validation and RequiredField Validator:-

<label for="email">CEmail</label>

   <asp:TextBox ID="txtcemail" runat="server" CssClass="txtfield"

                    > </asp:TextBox>

   <asp:RequiredFieldValidator ID="RequireValidator1"

             runat="server"

             ControlToValidate="txtcemail"

             Operator="Equal"

             ErrorMessage="Email can't be left blank"

             SetFocusOnError="True" Display="Dynamic"></asp:RequiredFieldValidator>

        <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server"

            ErrorMessage="email is not valid" ControlToValidate="txtcemail"

            Display="Dynamic"

            ValidationExpression="^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$"></asp:RegularExpressionValidator>


URL Validator or Regular Expression Validator:-

   <label for="email">CURL</label>

   <asp:TextBox ID="txtcurl" runat="server" CssClass="txtfield"

                     > </asp:TextBox>

        <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"

            ErrorMessage="url is not valid" ControlToValidate="txtcurl" Display="Dynamic"

            ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?"></asp:RegularExpressionValidator>


Required Field Validator:-

 <label for="email">CName</label>

    <asp:TextBox ID="txtclname" runat="server" CssClass="txtfield"

                   > </asp:TextBox>

<asp:RequiredFieldValidator ID="RequireValidator3"

             runat="server"

             ControlToValidate="txtclname"

             ErrorMessage="clname can't be left blank"

             SetFocusOnError="True" Display="Dynamic"></asp:RequiredFieldValidator>



 Mobile Number or Cell No Validation:

   <label for="email">Ccellno</label>

   <asp:TextBox ID="txtccellno" runat="server" CssClass="txtfield"

                    > </asp:TextBox>

           <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"

            ErrorMessage="only number is allowed" ControlToValidate="txtccellno"

            Display="Dynamic" ValidationExpression="^[0-9]{10}"></asp:RegularExpressionValidator>


      <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"

          ErrorMessage="email is not valid" ControlToValidate="txtEmailID"

          Display="Dynamic"

          ValidationExpression="^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$"></asp:RegularExpressionValidator>

   <label for="email">User Name</label>

    <asp:TextBox ID="txtunm" runat="server" CssClass="txtfield"> </asp:TextBox>

    

 Compare ValiDator:-

<label for="email">Password</label>

   <asp:TextBox ID="txtpwd1" runat="server" TextMode="Password" CssClass="txtfield"></asp:TextBox>


 <label for="email">Confirm Password</label>

   <asp:TextBox ID="txtcon" runat="server" TextMode="Password" CssClass="txtfield"></asp:TextBox>


      <asp:CompareValidator ID="CompareValidator1" runat="server"

          ErrorMessage="password does not matches" ControlToCompare="txtpwd1"

          ControlToValidate="txtcon" Display="Dynamic"></asp:CompareValidator>

 
RangeValidator Of Age:-

//You can insert only age between 18 to 60
         <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="RangeValidator" ControlToValidate="TextBox1" MaximumValue="18" MinimumValue="60" Type="Integer"></asp:RangeValidator>
    </div>

ZipCode Validator or Regular Expression Validator:-



  <label for="email">ZipCode</label>



   <asp:TextBox ID="txtzipcode" runat="server" CssClass="txtfield"



                     > </asp:TextBox>

        <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"

            ErrorMessage="url is not valid" ControlToValidate="txtzipcode" Display="Dynamic"

            ValidationExpression="^(\d{5}-\d{4}|\d{5}|\d{9})$|^([a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d)$"></asp:RegularExpressionValidator>

 


Strong Password Validator or Regular Expression Validator:- 



 <label for="email">Strong Password</label>



   <asp:TextBox ID="txtpassword" runat="server" CssClass="txtfield"



                     > </asp:TextBox>

        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"

            ErrorMessage="url is not valid" ControlToValidate="txtpassword" Display="Dynamic"

            ValidationExpression="(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$"></asp:RegularExpressionValidator>
 

        


        

        

0 comments:

Post a Comment