Welcome, guest | Sign In | My Account | Store | Cart

A bit of Regular Expressions to test whether a given string conforms to being an eMail address or comma-separated list of them.

JavaScript, 17 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
function checkEmail(eadd) {
eadd=eadd.replace(/^[\s]+/, ""); eadd=eadd.replace(/[\s]+$/, "");
if (eadd=="") {alert('Please enter an eMail address'); return false;}
var literal="[\x2D^!#$%&'*+/=?0-9A-Z_`a-z{|}~]+";
var quoted='"[\x20-\x7F]+"';
var word="("+literal+"|"+quoted+")";
var domn="([-0-9A-Z_a-z]+|"+quoted+")";
var emailAdd=word+"([.]"+word+")*@("+domn+"[.])+[a-zA-Z]{2,6}";
var litName="[\x2D\x20^!#$%&'*+/=?0-9A-Z_`a-z{|}~]+";
var quoName='"[\x00-\x09\x0E-\x21\u0023-\uFFFF]+"[\x20\t]*';
var emailFormat="("+emailAdd+"|("+litName+"|"+quoName+")<"+emailAdd+">)";
var emailList=emailFormat+"(,[\x20\t\n]*"+emailFormat+")*";
var emailValid=new RegExp("^"+emailList+"$");
if (emailValid.test(eadd)) return true;
else {
alert('The eMail address you entered has an invalid format. Please check it and try again');
return false; } }
Created by SoftMoon WebWare on Wed, 5 Feb 2014 (MIT)
JavaScript recipes (69)
SoftMoon WebWare's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks