Validate emailaddress

implementation of the regular expression which defines a correct email adress ^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@([_a-zA-Z0-9-]+\.)+([a-zA-Z][a-zA-Z]+)$ function checkEmail (e) { var i, j, l = e.length; var foundPoint = false; function checkChars (s, i, l) { while (i < l && ("_-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789").indexOf(s.charAt(i)) != -1){ i++; } return i; } function checkFirstLevelDomainChars (s, i, l) { while (i < l && ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").indexOf(s.charAt(j)) != -1) { i++; } return (i == l); } trace(e); // every email starts with a string if ((i=checkChars(e, 0, l)) == 0) { return -1; } //init j j=i; // followed by an arbitrary number of ("." string) combinations while (i < l && e.charAt(i) == ".") { // skip the point i++; // if there are no chars, we have an error if ((j=checkChars(e, i, l)) == i) { return -2; } // else skip the chars i = j; } // then follows the magic @ if (e.charAt(i) != "@"){ return -3; } // followed by minimum one string point string // after the last point minimum 2 characters are allowed do { // skip the @ (j == i at the beginning, so it is like i++) i = j+1; // do we have more chars ? j = checkChars(e, i, l); if (j == i) { // no more chars found -> error return -4; } else if (j == e.length) { // emailaddress is finished, do we have a first level domain ? j -= i; // we have one if it is at least 2 long and consists of the correct characters if(foundPoint && j>=2 && checkFirstLevelDomainChars(e, i, l)){ return 1 } else { return -5 } } // if we reach the end or don't have a point, we return an error foundPoint = (e.charAt(j) == "."); } while (i < l && foundPoint); return -6; } //usage trace(checkEmail("actionscript@bokelberg.de")); trace(checkEmail("test.test@test.com")); trace(checkEmail("test.test@test.co.uk")); trace(checkEmail("test.test@test.co.ukuk")); trace(checkEmail("test.test@.uk")); trace(checkEmail("test.test@test.co@uk")); trace(checkEmail("te}st.test@test.co@uk")); trace(checkEmail("www.moma.museum")); trace(checkEmail("postmaster@moma.museum")); trace(checkEmail("someone@de")); trace(checkEmail("someone@.")); trace(checkEmail("someone@de.")); trace(checkEmail("someone@.de"));
BJ Date: 14/12/2001
Seems to get stuck in a loop somewhere in the function and freezes up.
BVision Date: 27/12/2003
Thanks for the Functions, I was able to seemlessly apply it to my origional contact form function. Great Work. Works Great for Me. http://www.boundless-vision.com
Rickard Lundberg Date: 22/03/2004
I found what I think is a typo on this line "while (i < l && ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").indexOf(s.charAt(j)) != -1) {" replace "j" with "i" and it works better! -- Rickard
Mike Date: 16/03/2007
The script returns -5 when the user has 3 characters: aa@aa.com -> 1 aaa@aa.com -> -5 aaaa@aa.com -> 1
Mike Date: 16/03/2007
a@aaaaa.co.uk -> 1 aa@aaaaa.co.uk -> -5 aaa@aaaaa.co.uk -> 1
Kenny Date: 27/03/2007
Mike's false negatives are all fixed by fixing the typo mentioned by Rickard.
Eddie Saidani Date: 08/08/2007
Thanks for the script, it works very well. ;-)
braden wiks Date: 01/10/2007
thanks for the script, once the typo is fixed it works fantastic. will reference this page in my actionscript cheers
Terrence Date: 06/11/2007
Hi there, I have just found this page with this script and it sounds very comprehensive. I'm not very advanced with action scripts and don't exactly know how to include it into my simple email form? I have this script below just to check that my fields are not empty if so it stops if complete it posts and then clears the form: if (name eq "" or email eq "" or phone eq "" or message eq "") { stop(); } else { loadVariablesNum("email.php", 0, "POST"); name = ""; email=""; phone=""; message=""; stop(); } } where abouts would I include the validation script? Many thanks, Terrence
anonymous Date: 28/04/2008
Very nice, this helps the as2 users who dont have access to as3's regex library. Only question why didnt you make it just return true or false? Thanks!
alice Date: 17/06/2008
Re: Mike Date: 16/03/2007 a@aaaaa.co.uk -> 1 aa@aaaaa.co.uk -> -5 aaa@aaaaa.co.uk -> 1 As Mike pointed out, this code has small bug with incorrect validation of the domain. It will be fixed so: while (i < l && ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").indexOf(s.charAt(i)) != -1) {
fernando Date: 29/08/2008
Hi How do you place this into a button?
Add comment
Home