Forum Moderators: open
I need to check if email addresses in the form field are correct i.e. have @ and domain.com or subdomain.domain.com
syntax
Perhaps somebody here could help?
Normally I'm not a fan of doing anyone's homework, but I'll post this little snippet anyway. It's taken from a PHP script, but the same principle applies whether you use Perl/PHP/JavaScript/whatever. Just remember, if you use a client side solution (such as JavaScript) you should implement a server side solution as well. Anyway, here goes:
if(strlen($_POST['email'])<7 ¦¦
!preg_match("/^[-_.[:alnum:]]*[-_[:alnum:]]@((([[:alnum:]]¦
[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)*([[:alnum:]][[:alnum:]-]*
[[:alnum:]])\.(ad¦ae¦aero¦af¦ag¦ai¦al¦am¦an¦ao¦aq¦ar¦arpa¦as¦at¦
au¦aw¦az¦ba¦bb¦bd¦be¦bf¦bg¦bh¦bi¦biz¦bj¦bm¦bn¦bo¦br¦bs¦bt¦bv¦bw¦
by¦bz¦ca¦cc¦cd¦cf¦cg¦ch¦ci¦ck¦cl¦cm¦cn¦co¦com¦coop¦cr¦cs¦cu¦cv¦cx
¦cy¦cz¦de¦dj¦dk¦dm¦do¦dz¦ec¦edu¦ee¦eg¦eh¦er¦es¦et¦eu¦fi¦fj¦fk¦fm¦
fo¦fr¦ga¦gb¦gd¦ge¦gf¦gh¦gi¦gl¦gm¦gn¦gov¦gp¦gq¦gr¦gs¦gt¦gu¦gw¦gy¦
hk¦hm¦hn¦hr¦ht¦hu¦id¦ie¦il¦in¦info¦int¦io¦iq¦ir¦is¦it¦jm¦jo¦jp¦ke
¦kg¦kh¦ki¦km¦kn¦kp¦kr¦kw¦ky¦kz¦la¦lb¦lc¦li¦lk¦lr¦ls¦lt¦lu¦lv¦ly¦
ma¦mc¦md¦mg¦mh¦mil¦mk¦ml¦mm¦mn¦mo¦mp¦mq¦mr¦ms¦mt¦mu¦museum¦mv¦mw¦
mx¦my¦mz¦na¦name¦nc¦ne¦net¦nf¦ng¦ni¦nl¦no¦np¦nr¦nt¦nu¦nz¦om¦org¦
pa¦pe¦pf¦pg¦ph¦pk¦pl¦pm¦pn¦pr¦pro¦ps¦pt¦pw¦py¦qa¦re¦ro¦ru¦rw¦sa¦
sb¦sc¦sd¦se¦sg¦sh¦si¦sj¦sk¦sl¦sm¦sn¦so¦sr¦st¦su¦sv¦sy¦sz¦tc¦td¦tf
¦tg¦th¦tj¦tk¦tm¦tn¦to¦tp¦tr¦tt¦tv¦tw¦tz¦ua¦ug¦uk¦um¦us¦uy¦uz¦va¦
vc¦ve¦vg¦vi¦vn¦vu¦wf¦ws¦ye¦yt¦yu¦za¦zm¦zw)¦
(([0-9][0-9]?¦[0-1][0-9][0-9]¦[2][0-4][0-9]¦[2][5][0-5])\.){3}
([0-9][0-9]?¦[0-1][0-9][0-9]¦[2][0-4][0-9]¦[2][5][0-5]))$/i",
$_POST['email']) ¦¦ strpos($_POST['email'],".")===0) {
echo "* Please enter a valid e-mail address";
}
Note: The red lines must all be on one line!
if (myString.indexOf("@") > 0) { etc and build your own simple email checker using the same idea for . as well.
I did it myself once, luckily I've still got the code. This simple function checks that the e-mail address has an @ symbol, something before the @, and someting with a . in it after the @. It's not foolproof but it'll make sure people at least attempt to get their e-mail address right. The function accepts a string, and returns true or false.
function checkEmail(email) {
var good = true
var at = email.indexOf("@")
var dot = email.lastIndexOf(".")
if (email == "") { good = false }
if (at < 1) { good = false }
if (dot < at + 2) { good = false }
if (dot > (email.length - 3)) { good = false }
return good
} There are better ones than this available, but it's small and simple and a lot better than nothing. And like DrDoc said, you have to check server-side as well, in case the user has JavaScript turned off!
/^[\w-][\w-\.]+@[\w-][\w-]+(\.[a-zA-Z]{2,4})+$/
sould do for the lot.
will say true for me@domain.com or me@domain.co.uk
but will not say true to me@subdomain.domain.com
a change to
/^[\w-][\w-\.]+@[\w-][\w-]+(\.[\w-]+)*(\.[a-zA-Z]{2,4})+$/
sould work for me@subdomain.domain.com and me@subdomain.domain.co.uk
am I right?!
Panos
I guess it depends on the level of validation you need. On the client side, I wouldn't worry too much, but on the server side you should always use a more comprehensive function to validate the user submitted data.
I am really just a JS newbie so it's more a matter of learning rather than functional thing and certainly not homework:)))
I really don't do server side and just started html 4 months ago.
I'm much more into non-technical things like SEO but willing to be developer-independent:)
It is merely client-side really
I just need to give window.alert if email format is wrong or send them to a different page if it's right
What would you say to a variant like this (based on Purple Martin snippet):
function checkEmail(email) {
var at = email.indexOf("@")
var dot = email.lastIndexOf(".")
if (email == "") ¦¦ (at < 1) ¦¦ (dot < at + 2) ¦¦ (dot > (email.length - 3)) {
window.alert('Your email format is incorrect!')
}
else
{ window.navigate('http://www.somesite.com');
}}
Doesn't seem to work though...
Purple Martin,Your validator will fail if I enter an address such as me@foobar.se, which is a perfectly valid address ;)
Oh no it won't!
Have another look DrDoc, that address works just fine. As far as I can tell there aren't any fatal flaws with my validator (if there are I'd love to hear about them).
;)
window.alert('Your email format is incorrect!')
Don't use this sort of error message. Sooner or later you will release a piece of software that has a validation bug in it, and you will end up telling a user that the format of their email address is incorrect when they know full well that it isn't. They're liable to go away in anger.
Better is something like "I don't recognise your email address". This is more accurate, sounds less high-handed and, if it turns out that the routine does have a bug in it, gives you a chance of retreating gracefully, because it doesn't say who's right and who's wrong. It just points out a mismatch between what the user entered and what the routine expected.