Pages

Search This Blog

Monday 12 May 2014

Steps For writing Registration page and validations ......

Hi friends, today I will say about common validations that we need to do while creating a registration page....

Normally any registration page of any website has
  •         Username
  •        Password
  •         Re-Enter Password
  •         Email
  •         Phone Number

In some cases we need to fill the field compulsorily like “Password”. In such cases we need to use a tag called “RequiredFieldValidator”.

Let us take an example and see “How to show error message if the user doesn’t fill the specific field”

Here in my application user must specify compulsorily there email....                                   

<asp:Label ID="lb1" runat="server" Text="Email"> </asp:Label>
<asp:TextBox ID="tbemail" runat="server"></asp:TextBox>

<asp:RequiredFieldValidator ID="reqemail" runat="server" ControlToValidate="tbemail"     ErrorMessage="Enter Email Address">*</asp:RequiredFieldValidator>

Now we need to must check whether the entered value is valid or not.
Example we can take phone number. Every phone number has “10 digits only”. So we need to check that the person has entered 10 digits or more/less.

In asp.net we will check it by using “RegularExpressionValidator”.
·       For phone number
<asp:Label ID="lb1" runat="server" Text="Phone Number"> </asp:Label>

<asp:TextBox ID="tbphone" runat="server"></asp:TextBox>
                                       
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="tbphone"
 ErrorMessage="Please Enter 10 digit Mobile Number" ValidationExpression="^[0-9]{10}"></asp:RegularExpressionValidator>

·        For Email
<asp:Label ID="lb1" runat="server" Text="Email"> </asp:Label>

<asp:TextBox ID="tbemail" runat="server"></asp:TextBox>

<asp:RequiredFieldValidator ID="reqemail" runat="server" ControlToValidate="tbemail"   ErrorMessage="Enter Email Address">*</asp:RequiredFieldValidator>
                                      
<asp:RegularExpressionValidator ID="regemail" runat="server" ControlToValidate="tbemail"
 ValidationExpression="^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$" ErrorMessage="Invalid Mail ID"></asp:RegularExpressionValidator>

·        For Password and re-enter password :
To compare to textbox values we use the tag “CompareValidator”.
<asp:Label ID="lb1" runat="server" Text="Enter Password"> </asp:Label>

<asp:TextBox ID="tbpwd1" runat="server"
            TextMode="Password"></asp:TextBox>
                             
<asp:RequiredFieldValidator ID="reqpwd1" runat="server" ControlToValidate="tbpwd1                                   ErrorMessage="Enter Password">*</asp:RequiredFieldValidator>
                           
   
<asp:Label ID="lb1" runat="server" Text="Re-Enter Password"> </asp:Label>

<asp:TextBox ID="tbcpwd1" runat="server" TextMode="Password"></asp:TextBox>
            
<asp:RequiredFieldValidator ID="reqcpwd1" runat="server" ControlToValidate="tbcpwd1" ErrorMessage="Enter Confirm New Password">*</asp:RequiredFieldValidator>
                              
<asp:CompareValidator ID="cpv1" runat="server" ControlToCompare="tbpwd1" ControlToValidate="tbcpwd1"
ErrorMessage="Password not matched">*</asp:CompareValidator>



For showing error message we will use the tag “ValidationSummary”
<asp:ValidationSummary ID="VS1" runat="server" ShowSummary="false" ShowMessageBox="true" />

Here it will show error in a messagebox so we kept it as true.

Friend's you can rate my ASP.NET classes on right side of this page..... 

                                                                        -Your's Santoshklpkl

2 comments:

Animated Social Gadget - Blogger And Wordpress Tips Twitter Bird Gadget