kumiko,
I am going to assume that you can limit users to using the comma (,) for separating email addresses. If not, you'll need to adapt this code to exactly what you need. In any case, I made a code snippet for you that will show you how to split the list of emails into an array and iterate through them to validate. This should get you well on your way.
-----
<script language="javascript">
function emailsProcess (emails)
{
// Establish variables for email array and array length
var array = emails.split(",");
var length = array.length;
// Establish validity checking variables
var validCheck = false;
var validCount = length;
// Loop through array using length variable
for (var i=0; i<length; i++)
{
// Call the function to check email validity
validCheck = emailsValidate(array[i]);
// If the email is valid, deincrement the validity
// counter
if (validCheck)
{
validCount --;
}
}
// If the validity counter had deincremented back to
// zero (or less, though that shouldn't be possible)
// then we know all emails are valid
if (validCount <= 0)
{
// Return valid message to user; NOTE: You'll
// probably want to change this to do something
// else in your program
alert("All emails are valid!")
} else {
// Return invalid message to user; NOTE: You'll
// probably want to change this to do something
// else in your program
alert("Some or all emails are not valid...")
}
}
function emailsValidate (email)
{
// Establish variables checking for location of the
// "@" and "." characters in email address
var atPos = email.indexOf("@")
var stopPos = email.lastIndexOf(".")
// Establish a return value variable
var returnValue = false;
// Check if the needed characters exist in the address
if (atPos == -1 || stopPos == -1)
{
// If they don't, return a false value
returnValue = false;
} else {
// If they do, check whether the "@" occurs before
// the last "." occurence
if (atPos < stopPos)
{
// If it does, return a true value; Email is
// valid
returnValue = true;
} else {
// If it doesn't, return a false value
returnValue = false;
}
}
// Return the return value variable to the caller
return returnValue;
}
</script>
|