JRS: In article <lV************ *******@juliett .dax.net>, dated Thu, 3
Nov 2005 09:39:29, seen in news:comp.lang. javascript, Dag Sunde
<me@dagsunde.co m> posted :
I have this RegExp to to a very simple validation of an
email-address, but it turns out that it refuses to
accept mail-addresses with hypens in them.
Can anybody please help me adjust it so it will accept
addresses like da*******@test-domain.net too?
Here's what got:
function validateEmail(e Mail) {
return /^(\w+\.)*(\w+)@ (\w+\.)+([a-zA-Z]{2,4})$/.test(eMail);
}
It is pointless to attempt to validate an E-mail address format in
detail, unless you are writing an E-mail system, or unless the task is
purely pedagogical. A RegExp that will accept
me@dagsunde.com will also
accept
im@dogsendu.com, and for any given message at least one of those
is wrong. There's no way round it; someone typing an E-address just has
to be careful to get it right.
If a test does not accept any RFC-permitted E-address, there will be
complaints; if an allegedly-full test fails to reject any non-permitted
address, there will be complaints. And if the RFCs change the format
range, full-check code will have to be changed too.
Testing with /.+@.+\..+/ or /^.+@.+\..+$/, to ensure at least x@y.z,
does make sense as a check that something like an E-address has been put
in the field, rather than say a price or an arbitrary vulgarity.
See <URL:http://www.merlyn.demo n.co.uk/js-valid.htm#VEmA> .
--
John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.