Monday, September 27, 2010

Validate More than One Email Address in a TextBox In ASP.NET by using JavaScript



Single E-Mail Address Validation  using JavaScript

JavaScript Function:

 <script language = "Javascript">

       function Validateemail()
         {
             var emailaddress = document.getElementById('<%=txtEmail.ClientID %>').value;
              var isValidEmail = true;
               var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
               isValidEmail = emailPattern.test(emailaddress);
                 if (isValidEmail == false)
                {
                    alert("invalide Email Address");
                    return false;
                }
                else
                {
                    return true;
                }
              
          }
</script>

HTML Form Coding

<form name="frmSample" method="post" action="#" onSubmit="return Validateemail()">
                <p>Enter an Email Address :
                  <input type="text" name="txtEmail">
                </p>
                <p>
                  <input type="submit" name="Submit" value="Submit">
                </p>
              </form>

Check Functionality Below:

Enter an Email Address :





OUTPUT:

           After successfully validating the e-mail address it will navigate to another page. here it will shows "Method Call NotAllowed" because i didn't give any destination url of the form

Validating Mulitple emailAddress in ASP.NET by using  Javascript:

Script To Validate More than one email address separted by semicolon(;)

<script language = "Javascript">
{





{
isValidEmail = emailPattern.test(emailids[i]);

alert(

}
}



{
function Validateemail()var emailaddress = document.getElementById('<%=txtEmail.ClientID %>').value;var emailids=emailaddress .split(';');var isValidEmail = true;var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;for (var i = 0; i <= emailids.length; i++)if (isValidEmail == false) {"The Following Email Address is Invalid: " + emailids[i]);break;if (isValidEmail == false)// alert("invalide Email Address");
}
return false;else{

}

}
return true;</

<
script>


.ASPX Code


p>Enter an Email Address : <asp:TextBox runat="server" ID="txtEmail"></asp:TextBox></p>
<p> <input type="submit" name="Submit" value="Submit" onclick="if(!Validateemail()) return false;"></p>

No comments:

Post a Comment