Home } Forms } Extract Email Addresses



Visitors sometimes include their email address inside the message field when they fill out forms. To deal with this, this script parses through the string and uses a Regular Expression to find then return a comma-separated list of the valid email addresses it finds. Easy!

(For this demo, enter some text in the box and then click out of the field. If you included
one or more valid email addresses, they will be displayed in the Email field below the box.)


Email:

The JavaScript Source: Forms: Extract Email Addresses

Simply click inside the window below, use your cursor to highlight the script, and copy (type Control-c or Apple-c) the script into a new file in your text editor (such as Note Pad or Simple Text) and save (Control-s or Apple-s). The script is yours!!!

    

Did you use this script?  Do you like this site?  Please link to us!

Add a comment, suggestion, or correction
[For questions about usage, consult the Notes tab above or visit the JavaScript forum. Do not include more than two (2) lines of code in your comments. If you have suggestions or corrections, you can submit them to us.]

    
   
       
[optional]
 
[optional]

   

Comments feed Comment Feed RSS 2.0

1. From: jfinlayson
more valid email address characters
11/25/2008 17:45:03

StrObj.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);

Technically, there are several more valid characters that should included in this regular expression.

According to RFC 2822 (URL below), all of the following US-ASCII characters are valid in the local part (before the "@") of an email address:

A-Z
a-z
0-9
!#$%&'*+-/=?^_`{|}~

All of those except the dot character (.) are valid anywhere in the email address. The dot character may not appear as the first or last character in the local-part or domain.

http://www.faqs.org/rfcs/rfc2822.html
See section 3.4.1. for the definition of "addr-spec", and section 3.2.4 for the definition of "atext".

At the very least, I think the plus symbol (+) should be included. It is used by popular mail services (including GMail and FastMail) to support customized email addresses.

Sign up for the JavaScript Weekly

internet.com Privacy Policy
We'll send this script to you!

(just click "Send it!" once!)


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers