PDA

View Full Version : JS EMAIL Validation Explained


briancf
02-10-2003, 10:10 PM
I found a javascript function for validating user entered email addresses that seems to work by enforcing a single "@" and a single "." etc. I'm not comfortable using it until I can explain how it works. Confusing part is the IF statement coded as follows. Any hints or references to explain what is happening to the left side of the ".test" would be appreciated. Help Please!

Here's the function

function checkEmail(myForm) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.emailAddr.value)){
return (true)
}
alert("Invalid E-mail Address! Please re-enter.")
return (false)
}

Thanx in advance for any help.

jkd
02-10-2003, 10:40 PM
That is called a regular expression. There was a recent discussion on these forums regarding using RegExp to "validate" email addresses, and I seem to remember it to be generally decided that there were too many different permutations of syntax to effectively check against.

Just to confuse you some more, here an email RegExp I wrote a while ago which seems to be a little stricter than the one you have:

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

:)

briancf
02-11-2003, 12:57 PM
Thanks for the help and pointers.