JEYAGANESH

JEYAGANESH
Software Developer

Tuesday, 6 November 2012

JAVASCRIPT FORM VALIDATION FOR ASP.NET WEB FORM


<!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>::JavaScript Form Validation:: Jeyaganesh asp.net Developer</title>
    <script language="javascript" type="text/javascript">
   function validate() {
       if (document.getElementById("<%=TextBox1.ClientID%>").value == "") {
           alert("Name Feild can not be blank");
           document.getElementById("<%=TextBox1.ClientID%>").focus();
           return false;
       }

       if (document.getElementById("<%=TextBox2.ClientID %>").value == "") {
           alert("Email id Feild can not be blank");
           document.getElementById("<%=TextBox2.ClientID %>").focus();
           return false;
       }

       if (document.getElementById("<%=TextBox3.ClientID %>").value == "") {
           alert("Mobile No Feild can not be blank");
           document.getElementById("<%=TextBox3.ClientID %>").focus();
           return false;
       }
       if (document.getElementById("<%=TextBox4.ClientID %>").value == "") {
           alert("Feedback Feild can not be blank");
           document.getElementById("<%=TextBox3.ClientID %>").focus();
           return false;
       }
       var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
       var emailid = document.getElementById("<%=TextBox2.ClientID %>").value;
       var matchArray = emailid.match(emailPat);
       if (matchArray == null) {
           alert("Your Email Address Seems Incorrect. Please try again.");
           document.getElementById("<%=TextBox2.ClientID %>").focus();
           return false;
       }


       return true;
   }
</script>
</head>
<body>
    <form id="form1" action="" runat="server">
<table width="500" border="0" style="text-align:left; font-size:16px; color:#C6913D; font-weight:bold;">
  <tr>
    <td align="left">Enquiry Form</td>
    <td></td>
  </tr>
  <tr>
    <td>Name</td>
    <td>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
  </tr>
  <tr>
    <td>Email Id</td>
    <td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
  </tr>
  <tr>
    <td>Mobile No</td>
    <td><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td>
  </tr>
  <tr>
    <td>Comments</td>
    <td><asp:TextBox ID="TextBox4" runat="server" TextMode="MultiLine" Width="300px" Height="150px"></asp:TextBox></td>
  </tr>

  <tr>
    <td></td>
    <td><br/>
        <asp:Button ID="Button1" runat="server" Text="Submit"
             OnClientClick="return validate()"/>
    </td>
  </tr>
</table>
</form>
</body>
</html>

No comments:

Post a Comment