AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 How to validate multiple email addresses in a Textbox through JavaScript?
Posted by Moderator1 on  5/27/2010 9:53:46 AM 10115 Views
This code snippet will help you to validate group of email addresses in a multi-line text
Box. In a multi-line textbox, one email address is entered in a line, which can be separated using the carriage return (\n) delimiter. The separated emails are passed to a JavaScript function that uses Internet Email Address Regular Expression for validation. You can set any delimiters to separate the email addresses, accordingly you need to change the function to separate it.

Place the below JavaScript code between the html <head></head> tags. The function fnValidation is the main function that is called from the button click event. This function separates the email address in the multi-line text box and passed one email address at a time to another JavaScript function fnValidateEmail to validate it. If email address is valid, the fnValidateEmail function will return true, otherwise it will return false.


<script>
function fnValidateEmail(EmailId)
{
var EmailRegExp = "\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
var regex = new RegExp(EmailRegExp);

if (EmailId.match(regex))
return true;
else
return false;
}

function fnValidation()
{
var val = document.getElementById("TextBox1").value;
var newtext = val.split("\n");
for(var i=0 ;i < newtext.length; i++)
{
var validEmail = fnValidateEmail(newtext[i])
if (!validEmail)
{
alert("The list contains some Invalid Email ID.");
return false;
}
}

alert("All Email Address is Valid.");


}
</script>

Place a Textbox control and a Button control in the aspx page. Set the TextMode property of the TextBox as “MultiLine” and set the OnClientClick property of the button as “return fnValidation();”. The sample code is below.

<asp:TextBox ID="TextBox1" runat="server" Height="150px"
TextMode="MultiLine"></asp:TextBox>

<asp:Button ID="Button1" runat="server"
OnClientClick="return fnValidation();" Text="Validate Email" />

Post New Tips/Tricks View all Tips/Tricks

Featured Resources:
No Nonsense XML Web Development With PHP - Free 146 Page Preview!
Learn how to put XML to practical use on your Website.
SearchSAP
The Web's best SAP-specific information resource for enterprise IT professionals.
100% Free Subscription until Stock lost.View complete list.
Sponsored by