Saturday, 18 January 2014

Registration Form Validation using Java Script Regular Expression in Asp.Net

Ans:-

In Register.aspx :-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Register" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
       <script>
           function CheckValidation() {
               var userName = document.getElementById("txtName").value;
               var pwd = document.getElementById("txtPwd").value;
               var cPwd = document.getElementById("txtCPwd").value;
               var mob = document.getElementById("txtMobile").value;
               var email = document.getElementById("txtEmail").value;

               var regxUserNm = /^[A-z]+$/;
               var regxMob = /^[8-9][0-9]{9}$/;
               var regxEmail = /^[A-z]{3,5}[\@][A-z]{3,5}[\.]{1}[A-z]{2,3}$/;

               if (regxUserNm.test(userName) != true) {
                   alert("Invalid UserName");
                   return false;
               }
               if (regxEmail.test(email) != true) {
                   alert("Invalid EmailId");
                   return false;
               }
               if (regxMob.test(mob) != true) {
                   alert("Invalid PhoneNo");
                   return false;
               }
               if (pwd == "") {
                   alert("Enter Password");
                   return false;
               }
               if (pwd !=cPwd) {
                   alert("Enter Confirm password must be same as Pwd");
                   return false;
               }


           }
   
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <center>
    <div>
   
    Rgistration Form
     <table>
    <tr>
    <td>Name </td><td>:</td><td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
       
    </tr>
        <tr>
    <td>Email Id </td><td>:</td><td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
       
    </tr>
        <tr>
    <td>Mobile No </td><td>:</td><td><asp:TextBox ID="txtMobile" runat="server"></asp:TextBox></td>
       
    </tr>
        <tr>
    <td>Password </td><td>:</td><td><asp:TextBox ID="txtPwd" runat="server" Text=""></asp:TextBox></td>
       
    </tr>
        <tr>
    <td>Confirm Password </td><td>:</td><td><asp:TextBox ID="txtCPwd" runat="server"></asp:TextBox></td>
       
    </tr>
    <tr>
    <td colspan=3><asp:Button ID="btnRegister" runat="server" Text="Register" OnClientClick="CheckValidation()"  /></td>
    </tr>
       
   
    </table>
   
    </div>
    </center>

    </form>
</body>
</html>


No comments:

Post a Comment