Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 6th, 2008, 05:23 AM
Authorized User
 
Join Date: Aug 2006
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default Multiple emails validation in one text box with co

hi there..i need some help on this email validation. i got a text box, can let the users to input many email with the seprator comma(,) or this (. then how can i use the javascxript to do a validation for it? plz help..i duno how to code it. plz help on code.thanx alot

 
Old February 6th, 2008, 06:58 AM
Friend of Wrox
 
Join Date: Oct 2007
Posts: 130
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via AIM to urtrivedi
Default

For one email address you can do following

isEmail1 = /^\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w+$/;
isEmail2 = /^.*@[^_]*$/;

function isEmail(s)
{
  return (isEmail1.test(s) && isEmail2.test(s));
}

--call above code in validate event(such as button click or submit)
  if (!isEmail(document.txtemail.value))
  {
    alert('* a valid email address\n');
    return false;
  }

for multiple email in one text box
user can either use , or ; not both
first split the value of text box using , or ;
after spliting you will get array of emails
call above function by going through all emails in array through loop



urt

Help yourself by helping someone.
 
Old February 6th, 2008, 07:40 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I would replace the separators with a single one. Use the replace() function twice and pick a character that won't appear, e.g. |. The split on | and use your regular validation.
So
1) Replace , with |
2) Replace ( with |
3) split() on |
4) Validate each array item

--

Joe (Microsoft MVP - XML)
 
Old February 6th, 2008, 12:09 PM
Authorized User
 
Join Date: Oct 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

"can let the users to input many email with the seprator comma(,) or this (."

Do you mean to say that the user can use "," or "(." to separate the email addresses? I suppose I am confused as to why "(." would be a valid separator?
 
Old February 6th, 2008, 01:09 PM
Authorized User
 
Join Date: Oct 2003
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>
 
Old February 7th, 2008, 05:49 AM
Authorized User
 
Join Date: Aug 2006
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sorry ...the seperator is comma(,) OR (;)..soli for wrong typing!






Similar Threads
Thread Thread Starter Forum Replies Last Post
send emails with an Excel table as text format peterlihh Excel VBA 4 October 29th, 2008 04:04 PM
Multiple emails with one DTS package ninel SQL Server DTS 1 October 23rd, 2006 08:00 AM
Auto-Filling a Text box based on Multiple Boxes zuldan23 Access VBA 1 May 5th, 2006 10:17 AM
Access locks up after multiple emails sent Paulsh Access VBA 2 December 30th, 2005 03:20 PM
validation of text box isheikh Javascript 2 April 18th, 2004 12:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.