View Full Version : Validating Email address !!!

06-19-2002, 08:33 PM
Hi people,

I have found this script for validating an email address entered into a form field.
Yet it seems in complete. When you enter e.g. "[email protected]". It works. But when you put "[email protected]" it still works. So it needs a bit adding to it. In the script it checks to make sure there are characters after the '.', but it needs a section to check that there are characters before the '.' also. I have tried figuring it out, but to no evail. Any one help me with this little addition.

Thanks in advance

:D :D

:confused: oops, forgot the code the first time round when posting, here it is !!!!!

function validEmail(email) {
invalidChars = " /:,;"

if (email == "") { // Cannot be empty
return false
for (i=0; i<invalidChars.length; i++) { // Does it contain any invalid characters
badChar = invalidChars.charAt(i)
if (email.indexOf(badChar,0) > -1) {
return false
atPos = email.indexOf("@",1) // There must be at least one '@' symbol
if (atPos == -1) {
return false
if (email.indexOf("@",atPos+1) != -1) { // And only one '@' symbol
return false
periodPos = email.indexOf(".",atPos) // at least one '.' after the '@'
if (periodPos == -1) {
return false
if (periodPos+3 > email.length) { // must be at least 2 characters after the '.'
return false
return true

06-19-2002, 09:27 PM
function check(eml) {
invalidChars = " /:,;"

msg = "";

for (x=0; x<invalidChars.length; x++) {
xchar = invalidChars.charAt(x);
xchar2 = eml.indexOf(xchar);
if (xchar2!=-1) {
msg += "Invalid character "+xchar+" used.\n";

eml2 = eml.split("@");
l = eml2.length;
if (l<2) {
msg += "Missing \"@\" symbol.\n";
else if (l>2) {
msg += "Cannot have more than one \"@\" symbol.\n";
eml3 = eml2[1].split(".");
l2 = eml3.length;
if (l2<2) {
msg += "Missing \".\" symbol.\n";
else if (l2>2) {
msg += "Cannot have more than one \".\" symbol in the sitename.\n";
if (eml3[0]=="") {
msg += "Invalid site name.\n";
else if (eml3[1]=="") {
msg += "invalid site extension.\n";
if (msg != "") {
alert("Valid email");

06-19-2002, 10:32 PM
The smallest I could get a basic test that checks for
[character(s)] @ [character(s)] . [character(s)]


em = document.forms["reqs"].elements["email"].value;
tr = (em.indexOf("@",1)>0) ? em.indexOf(".",3) : -1;
if (tr<3) {
alert('bad email');

I'm sure a regex will do better though. :)

06-19-2002, 10:44 PM
Originally posted by Íkii
I'm sure a regex will do better though. :)
actually, somebody posted an email validation script using regexp on old boards a ong time ago. i think it was jkd. i only wished i had it saved. may be somebody has it.

06-19-2002, 11:05 PM
There is a regx one in this post:


06-19-2002, 11:42 PM
How about http://www.javascriptkit.com/script/script2/acheck.shtml


It contains the monster RegExp I wrote a while ago when I was bored:

(I'm quite proud of that beast ;))

06-20-2002, 12:48 AM
That's a good one, jkd - but it said a friend of mine's email address, i.e.

[email protected]

was invalid... from what I understand (correct me if I'm wrong!) email addresses should also allow a hyphen in the first part before the @ ?

Anyway this one seems to work good:

/^[a-z0-9]+([-_\.]?[a-z0-9])[email protected][a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}$/i

06-20-2002, 02:28 AM
Ah, \w doesn't incorpoate hyphens, thanks for the tip. (Didn't think of email addies with hyphens):

var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

Fixes that (I designed the RegExp especially to be used like:

re('[email protected]') == ['[email protected]', 'user', 'domain'. 'ext'];

(or re.exec(str) if that's more comfortable syntax, but in actuality all RegExp instances are really functions, interestingly enough)

That helps in validating certain parts of the address... :)

06-20-2002, 02:40 AM
I'm still not totally convinced on the actual validation rules for email addresses, though...

Does anyone have a comprehensive article on what exactly a valid email address is?!?

I've looked and looked (since long before this post, fyi) and still... I've never found a comprehensive resource.

Thanks in advance.

06-20-2002, 02:53 AM
I'm pretty sure it just consists of a valid *nix username, @, a valid subdomain/domain combination, a ., then 2-6 letters, then optionally followed by a dot and 2 letters.

06-20-2002, 11:08 AM
Hey thanks everyone for all your replys, I've put together a good email checker now from all of your input. Those regular expressions look pretty useful, must look into them abit more.

Anyway thanks very much :thumbsup: :thumbsup:

06-20-2002, 12:23 PM
Hi everyone,

Im trying to create a regular expression to validate a phone number. Here is what I got:

var te = /^([\d]+[?:\s][\d]+)$/

if (!te.test(Email_Form.Telephone.value)) {
alert ("Please enter your contact telephone number then re-submit the form")
return false

What it is suppos to do, is limit the input to just digits, which it does, but to allow a space character, if the user choices to pu one in. So in which case, this: 01333 844444 would be ok, so would 01333844444. It only seems to work when a space is there.

Any ideas ??

Thanks :confused:

06-20-2002, 01:05 PM
Ok, not to worry people, I figure my last problem out.

Here is what I should of put:


Thanks anyway :thumbsup: