function emailCheck (emailStr) {
emailStr = emailStr.toLowerCase();
/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|cc|tt|tv|inc|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Ths username contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths domain name contains invalid characters.");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

alert("The username doesn't seem to be valid.");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Destination IP address is invalid!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("The domain name does not seem to be valid.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}





<!-- Original:  Simon Tneoh (tneohcb@pc.jaring.my) -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
var Cards = new makeArray(8);
Cards[0] = new creditcardtype("MasterCard", "51,52,53,54,55", "16");
var MasterCard = Cards[0];
Cards[1] = new creditcardtype("VisaCard", "4", "13,16");
var VisaCard = Cards[1];
Cards[2] = new creditcardtype("AmExCard", "34,37", "15");
var AmExCard = Cards[2];
Cards[3] = new creditcardtype("DinersClubCard", "30,36,38", "14");
var DinersClubCard = Cards[3];
Cards[4] = new creditcardtype("DiscoverCard", "6011", "16");
var DiscoverCard = Cards[4];
Cards[5] = new creditcardtype("enRouteCard", "2014,2149", "15");
var enRouteCard = Cards[5];
Cards[6] = new creditcardtype("JCBCard", "3088,3096,3112,3158,3337,3528", "16");
var JCBCard = Cards[6];
var LuhnCheckSum = Cards[7] = new creditcardtype();

/*************************************************************************\
Checkcardnumber(form)
function called when users click the "check" button.
\*************************************************************************/
function Checkcardnumber() {
var tmpyear;

if (checkout.cardnumber.value.length == 0) {
alert("Please enter a Card Number.");
checkout.cardnumber.focus();
return false;
}
if (checkout.expirationyear.value.length == 0) {
alert("Please enter the Expiration Year.");
checkout.expirationyear.focus();
return false;
}
if (checkout.expirationyear.value > 96)
tmpyear = "19" + checkout.expirationyear.value;
else if (checkout.expirationyear.value < 21)
tmpyear = "20" + checkout.expirationyear.value;
else {
alert("The Expiration Year is not valid.");
return;
}
tmpmonth = checkout.expirationmonth.options[checkout.expirationmonth.selectedIndex].value;
// The following line doesn't work in IE3, you need to change it
// to something like "(new creditcardtype())...".
// if (!creditcardtype().isExpiryDate(tmpyear, tmpmonth)) {
if (!(new creditcardtype()).isExpiryDate(tmpyear, tmpmonth)) {
alert("This card has already expired.");
return false;
}
card = checkout.creditcardtype.options[checkout.creditcardtype.selectedIndex].value;
var retval = eval(card + ".checkcardnumber(\"" + checkout.cardnumber.value +
"\", " + tmpyear + ", " + tmpmonth + ");");
cardname = "";
if (retval)



// comment this out if used on an order form
//alert("This card number appears to be valid.");
return;

else {
// The cardnumber has the valid luhn checksum, but we want to know which
// creditcardtype it belongs to.
for (var n = 0; n < Cards.size; n++) {
if (Cards[n].checkcardnumber(checkout.cardnumber.value, tmpyear, tmpmonth)) {
cardname = Cards[n].getcreditcardtype();
break;
   }
}
if (cardname.length > 0) {
alert("This looks like a " + cardname + " number, not a " + card + " number.")
return false;
}
else {
alert("This card number is not valid.")
return false;
      }
   }
}
/*************************************************************************\
Object creditcardtype([String creditcardtype, String rules, String len, int year, 
                                        int month])
creditcardtype    : type of card, eg: MasterCard, Visa, etc.
rules       : rules of the cardnumber, eg: "4", "6011", "34,37".
len         : valid length of cardnumber, eg: "16,19", "13,16".
year        : year of expiry date.
month       : month of expiry date.
eg:
var VisaCard = new creditcardtype("Visa", "4", "16");
var AmExCard = new creditcardtype("AmEx", "34,37", "15");
\*************************************************************************/
function creditcardtype() {
var n;
var argv = creditcardtype.arguments;
var argc = creditcardtype.arguments.length;

this.objname = "object creditcardtype";

var tmpcreditcardtype = (argc > 0) ? argv[0] : "CardObject";
var tmprules = (argc > 1) ? argv[1] : "0,1,2,3,4,5,6,7,8,9";
var tmplen = (argc > 2) ? argv[2] : "13,14,15,16,19";

this.setcardnumber = setcardnumber;  // set cardnumber method.
this.setcreditcardtype = setcreditcardtype;  // setcreditcardtype method.
this.setLen = setLen;  // setLen method.
this.setRules = setRules;  // setRules method.
this.setExpiryDate = setExpiryDate;  // setExpiryDate method.

this.setcreditcardtype(tmpcreditcardtype);
this.setLen(tmplen);
this.setRules(tmprules);
if (argc > 4)
this.setExpiryDate(argv[3], argv[4]);

this.checkcardnumber = checkcardnumber;  // checkcardnumber method.
this.getExpiryDate = getExpiryDate;  // getExpiryDate method.
this.getcreditcardtype = getcreditcardtype;  // getcreditcardtype method.
this.iscardnumber = iscardnumber;  // iscardnumber method.
this.isExpiryDate = isExpiryDate;  // isExpiryDate method.
this.luhnCheck = luhnCheck;// luhnCheck method.
return this;
}

/*************************************************************************\
boolean checkcardnumber([String cardnumber, int year, int month])
return true if cardnumber pass the luhncheck and the expiry date is
valid, else return false.
\*************************************************************************/
function checkcardnumber() {
var argv = checkcardnumber.arguments;
var argc = checkcardnumber.arguments.length;
var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
var year = (argc > 1) ? argv[1] : this.year;
var month = (argc > 2) ? argv[2] : this.month;

this.setcardnumber(cardnumber);
this.setExpiryDate(year, month);

if (!this.iscardnumber())
return false;
if (!this.isExpiryDate())
return false;

return true;
}
/*************************************************************************\
String getcreditcardtype()
return the creditcardtype.
\*************************************************************************/
function getcreditcardtype() {
return this.creditcardtype;
}
/*************************************************************************\
String getExpiryDate()
return the expiry date.
\*************************************************************************/
function getExpiryDate() {
return this.month + "/" + this.year;
}
/*************************************************************************\
boolean iscardnumber([String cardnumber])
return true if cardnumber pass the luhncheck and the rules, else return
false.
\*************************************************************************/
function iscardnumber() {
var argv = iscardnumber.arguments;
var argc = iscardnumber.arguments.length;
var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
if (!this.luhnCheck())
return false;

for (var n = 0; n < this.len.size; n++)
if (cardnumber.toString().length == this.len[n]) {
for (var m = 0; m < this.rules.size; m++) {
var headdigit = cardnumber.substring(0, this.rules[m].toString().length);
if (headdigit == this.rules[m])
return true;
}
return false;
}
return false;
}

/*************************************************************************\
boolean isExpiryDate([int year, int month])
return true if the date is a valid expiry date,
else return false.
\*************************************************************************/
function isExpiryDate() {
var argv = isExpiryDate.arguments;
var argc = isExpiryDate.arguments.length;

year = argc > 0 ? argv[0] : this.year;
month = argc > 1 ? argv[1] : this.month;

if (!isNum(year+""))
return false;
if (!isNum(month+""))
return false;
today = new Date();
expiry = new Date(year, month);
if (today.getTime() > expiry.getTime())
return false;
else
return true;
}

/*************************************************************************\
boolean isNum(String argvalue)
return true if argvalue contains only numeric characters,
else return false.
\*************************************************************************/
function isNum(argvalue) {
argvalue = argvalue.toString();

if (argvalue.length == 0)
return false;

for (var n = 0; n < argvalue.length; n++)
if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
return false;

return true;
}

/*************************************************************************\
boolean luhnCheck([String cardnumber])
return true if cardnumber pass the luhn check else return false.
Reference: http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl
\*************************************************************************/
function luhnCheck() {
var argv = luhnCheck.arguments;
var argc = luhnCheck.arguments.length;

var cardnumber = argc > 0 ? argv[0] : this.cardnumber;

if (! isNum(cardnumber)) {
return false;
  }

var no_digit = cardnumber.length;
var oddoeven = no_digit & 1;
var sum = 0;

for (var count = 0; count < no_digit; count++) {
var digit = parseInt(cardnumber.charAt(count));
if (!((count & 1) ^ oddoeven)) {
digit *= 2;
if (digit > 9)
digit -= 9;
}
sum += digit;
}
if (sum % 10 == 0)
return true;
else
return false;
}

/*************************************************************************\
ArrayObject makeArray(int size)
return the array object in the size specified.
\*************************************************************************/
function makeArray(size) {
this.size = size;
return this;
}

/*************************************************************************\
creditcardtype setcardnumber(cardnumber)
return the creditcardtype object.
\*************************************************************************/
function setcardnumber(cardnumber) {
this.cardnumber = cardnumber;
return this;
}

/*************************************************************************\
creditcardtype setcreditcardtype(creditcardtype)
return the creditcardtype object.
\*************************************************************************/
function setcreditcardtype(creditcardtype) {
this.creditcardtype = creditcardtype;
return this;
}

/*************************************************************************\
creditcardtype setExpiryDate(year, month)
return the creditcardtype object.
\*************************************************************************/
function setExpiryDate(year, month) {
this.year = year;
this.month = month;
return this;
}

/*************************************************************************\
creditcardtype setLen(len)
return the creditcardtype object.
\*************************************************************************/
function setLen(len) {
// Create the len array.
if (len.length == 0 || len == null)
len = "13,14,15,16,19";

var tmplen = len;
n = 1;
while (tmplen.indexOf(",") != -1) {
tmplen = tmplen.substring(tmplen.indexOf(",") + 1, tmplen.length);
n++;
}
this.len = new makeArray(n);
n = 0;
while (len.indexOf(",") != -1) {
var tmpstr = len.substring(0, len.indexOf(","));
this.len[n] = tmpstr;
len = len.substring(len.indexOf(",") + 1, len.length);
n++;
}
this.len[n] = len;
return this;
}

/*************************************************************************\
creditcardtype setRules()
return the creditcardtype object.
\*************************************************************************/
function setRules(rules) {
// Create the rules array.
if (rules.length == 0 || rules == null)
rules = "0,1,2,3,4,5,6,7,8,9";
  
var tmprules = rules;
n = 1;
while (tmprules.indexOf(",") != -1) {
tmprules = tmprules.substring(tmprules.indexOf(",") + 1, tmprules.length);
n++;
}
this.rules = new makeArray(n);
n = 0;
while (rules.indexOf(",") != -1) {
var tmpstr = rules.substring(0, rules.indexOf(","));
this.rules[n] = tmpstr;
rules = rules.substring(rules.indexOf(",") + 1, rules.length);
n++;
}
this.rules[n] = rules;
return this;
}
//  End -->

