Thursday, January 26, 2012

validate a Form in .Net

JavaScript Code:
<script language="javascript">
function validatefun()
{
     if (document.getElementById("<%=TextBox1.ClientID%>").value=="")
      {
                 alert("Name cannot be blank!");
                 document.getElementById("<%=TextBox1.ClientID%>").focus();
                 return false;
      }
      
      if(document.getElementById("<%=TextBox2.ClientID %>").value=="")
      {
                 alert("Email id cannot be blank!");
                document.getElementById("<%=TextBox2.ClientID %>").focus();
                return false;
      }
     var Email= /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
     var emailid=document.getElementById("<%=TextBox2.ClientID %>").value;
     var matchArr= emailid.match(Email);
     if (matchArr == null)
    {
               alert("Your email address are incorrect. So please try Again!");
               document.getElementById("<%=TextBox2.ClientID %>").focus();
               return false;
    }     
     return true;
}
</script>
 

HTML Code:
  Name: <asp:TextBox ID="TextBox1" runat="server" Width="100px"/>
  Email ID: <asp:TextBox ID="TextBox2" runat="server" Width="100px" 
            ToolTip="Enter Email Id" AutoPostBack="True"/>
 
<asp:Button ID="BtnSubmit" runat="server" Text="Submit" onclick=" BtnSubmit_Click"
                        OnClientClick="return validatefun() " />

No comments: