|
|
|
A pretty good
pragmatic Email validation code |
||
|
With respect to RFC2821. The character [@] devides the Email address into two parts - the local part and the domain part. In: [I.Am.Beautiful@Do.You.not.think.so.com] the starting sentence [I.Am.Beautiful] is the local part, and [Do.You.not.think.so.com] is the domain part. RFC2821 allows a lot of different special characters in the local part, but in practice everyone uses only [.] [_] [-], because all servers accept them. For example [+] is allowed, but not supported everywhere, so nobody uses it. In the domain part only [.] and [-] are allowed by RFC2821. Of course 0-9 and a-z and A-Z are allowed without restrictions everywhere. According to RFC2821 the local part of the Email-address is case sensitive, but - again - in practice nobody supports this! THE FOLLOWING CODE IS THE PRAGMATIC VERSION OF RFC2821 ! function CheckEmail(Email) { // The following characters are allowed: var Alfa="abcdefghijklmnopqrstuvwxyz0123456789-._@" // (Upper-Case is converted to Lower-Case later on) // The following special characters are allowed: var Chs="-._@" var Ch, Pos, Pos1 // Is the Email address at least 5 characters of length [s@g.z] // as expected? if (Email.length <5) { alert("E-mail-adressen er ikke korrekt [0]") // In English: The Email address is not correct return false } // The Email address is converted to lower-case letters as it is // not case sensative in practice: Email=Email.toLowerCase(); // Do we find [.] in the Email address as expected? if (Email.indexOf(".")==-1) { alert("E-mail-adressen er ikke korrekt [1]") return false } // Do we find more than one [@] in the Email address as not // expected? (The [Pos] is used later on) Pos=Email.indexOf("@") Pos1=Email.lastIndexOf("@") if (Pos != Pos1) { alert("E-mail-adressen er ikke korrekt [2]") return false } // Does the domain part of the Email address contain [_] as not // expected: (which is the only difference between the two parts // in practice) if (Email.indexOf("_",Pos+1) > -1) { alert("E-mail-adressen er ikke korrekt [3]") return false } // Does the Email address contain not allowed characters as not // expected? for (i=0; i<Email.length; i++) { Ch=Email.charAt(i) if (Alfa.indexOf(Ch)==-1) { alert("E-mail-adressen er ikke korrekt [4]") return false } } // Does the Email address start with a special character as not // expected? Ch=Email.charAt(0) if (Chs.indexOf(Ch)>-1) { alert("E-mail-adressen er ikke korrekt [5]") return false } // Does the Email address end up with a special character as not // expected? Ch=Email.charAt(Email.length-1) if (Chs.indexOf(Ch)>-1) { alert("E-mail-adressen er ikke korrekt [6]") return false } // Is two (or more) special characters neighbours as not // expected? for (i=0; i<Email.length; i++) { Ch=Email.charAt(i) if (Chs.indexOf(Ch) > -1) { Pos=Email.indexOf(Ch) Ch=Email.charAt(Pos+1) if (Chs.indexOf(Ch) > -1) { alert("E-mail-adressen er ikke korrekt [7]") return false } } } // Do we have more than 4 characters after the last [.] as not // expected? Pos=Email.lastIndexOf(".") if (Email.length-Pos>5) { alert("E-mail-adressen er ikke korrekt [8]") return false } } |
||
|
|