Thursday, 23 December 2010

Client side validation using javascript

<script language="javascript" type="text/javascript">
function validate2()
{
     
      if (document.getElementById("<%=txtName.ClientID%>").value=="")
      {
                 alert("Enter First Name");
                 document.getElementById("<%=txtName.ClientID%>").focus();
                 return false;
      }
                 
      if (document.getElementById("<%=txtEmail.ClientID%>").value=="")
      {
                 alert("Enter Email");
                 document.getElementById("<%=txtEmail.ClientID%>").focus();
                 return false;
      }
        var emailPat = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;   
        var emailid=document.getElementById("<%=txtEmail.ClientID %>").value;
        var matchArray = emailid.match(emailPat);
        if (matchArray == null)
        {               
                   alert("Enter Correct Email");
                   document.getElementById("<%=txtEmail.ClientID %>").focus();
                   return false;
        }
       
        if (document.getElementById("<%=txtSubject.ClientID%>").value=="")
      {
                 alert("Enter Subject");
                 document.getElementById("<%=txtSubject.ClientID%>").focus();
                 return false;
      }
        return true;
}
</script>

<table width="90%" border="0" align="center" cellpadding="2" cellspacing="0">
                                        <tr>
                                            <td width="30%">
                                                <strong>Name :</strong></td>
                                            <td width="70%">
                                                <asp:TextBox ID="txtName" runat="server" CssClass="text_field" Style="width: 250px"></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <strong>Email ID :</strong></td>
                                            <td>
                                                <asp:TextBox ID="txtEmail" runat="server" CssClass="text_field" Style="width: 250px"></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <strong>Subject :</strong></td>
                                            <td>
                                                <asp:TextBox ID="txtSubject" runat="server" CssClass="text_field" Style="width: 250px"></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <strong>Description :</strong></td>
                                            <td>
                                                <asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine" CssClass="text_field"
                                                    Style="width: 350px"></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                &nbsp;</td>
                                            <td>
                                                &nbsp;</td>
                                        </tr>
                                        <tr>
                                            <td>
                                                &nbsp;</td>
                                            <td>
                                                <asp:ImageButton ID="imgbtnSend" runat="server" OnClientClick="return validate2()" ImageUrl="images/send.png" Width="46"
                                                    Height="24" border="0" OnClick="imgbtnSend_Click" />
                                                <asp:ImageButton ID="imgbtnReset" runat="server" ImageUrl="images/send.png" Width="46"
                                                    Height="24" border="0" OnClick="imgbtnReset_Click" />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                &nbsp;</td>
                                            <td>
                                                &nbsp;</td>
                                        </tr>
                                    </table>

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger