Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 6th, 2008, 05:23 AM
Authorized User
 
Join Date: Aug 2006
Location: , , .
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

Reply With Quote
  #2 (permalink)  
Old February 6th, 2008, 06:58 AM
Friend of Wrox
 
Join Date: Oct 2007
Location: , , .
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.
Reply With Quote
  #3 (permalink)  
Old February 6th, 2008, 07:40 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #4 (permalink)  
Old February 6th, 2008, 12:09 PM
Authorized User
 
Join Date: Oct 2003
Location: Denver, CO, USA.
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?
Reply With Quote
  #5 (permalink)  
Old February 6th, 2008, 01:09 PM
Authorized User
 
Join Date: Oct 2003
Location: Denver, CO, USA.
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>
Reply With Quote
  #6 (permalink)  
Old February 7th, 2008, 05:49 AM
Authorized User
 
Join Date: Aug 2006
Location: , , .
Posts: 65
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:58 AM.


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