checking email address for @ and . characters
|
|
Thread rating:  |
libsfan01 - 24 Jun 2006 13:00 GMT Hi all!
Can anyone show me how to check and email field on a form for the existence of these two characters.
Kind regards
Marc
Bart Van der Donck - 24 Jun 2006 13:18 GMT > Can anyone show me how to check and email field on a form for the > existence of these two characters. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript"> // validates the format somechars@otherchars.yetotherchars function cM() { /.+@.+\..+/.test(document.mF.email.value) ? alert('OK') : alert('not OK'); } </script> </head> <body> <form name="mF" action="script.php"> <input type="text" name="email"> <input type="button" value="Check!" onClick="cM()"> </form> </body> </html>
(thanks Dr John Stockton, http://www.merlyn.demon.co.uk/js-valid.htm#VEmA)
 Signature Bart
Rik - 24 Jun 2006 13:36 GMT > function cM() { > /.+@.+\..+/.test(document.mF.email.value) ? > alert('OK') : alert('not OK'); > } Or /^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{2,4}$/i
Allthough I'm not sure which characters are actually allowed in an emailadress.
With the above code .@...%0dBcc:spam@example.net;more@example.net seems a totally valid emailadress.. Then again, I don't know where the validation is for, it could be sufficient.
Grtz,
 Signature Rik Wasmus
Noggon - 24 Jun 2006 14:12 GMT > > function cM() { > > /.+@.+\..+/.test(document.mF.email.value) ? [quoted text clipped - 3 lines] > Or > /^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{2,4}$/i It is possible to have a one chracter domain name now or soon will be, and a 5 character domain name too.
eg: .travel
Rik - 24 Jun 2006 14:43 GMT >>> function cM() { >>> /.+@.+\..+/.test(document.mF.email.value) ? [quoted text clipped - 6 lines] > It is possible to have a one chracter domain name now or soon will be, > and a 5 character domain name too. check, /^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{1,6}$/i
Now, who can shed some light on allowed characters in emailadresses?
Grtz,
 Signature Rik Wasmus
Bart Van der Donck - 24 Jun 2006 16:11 GMT > [...] > check, > /^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{1,6}$/i > > Now, who can shed some light on allowed characters in emailadresses? http://en.wikipedia.org/wiki/Email_address#Limitations
 Signature Bart
Rik - 24 Jun 2006 16:33 GMT >> [...] >> check, [quoted text clipped - 3 lines] > > http://en.wikipedia.org/wiki/Email_address#Limitations Hmmz, is someone can elaborate on the _exact_ meaning of 'any hyphen of the ascii character set'. Maybe it's the language difference, but if I translate hyphen the only one i know is '-'....
So, /^[0-9a-z!#$%&'*+\-\/=?^_`{|}~]+(\.*[0-9a-z!#$%&'*+\-\/=?^_`{|}~]+)*@([0-9a- z\-]+\.)+[a-z]{1,6}$/i
Then again, let's not go as far as Friedl's tester, quoted emailadresses are possible, a terrible, terrible thing to adjust for within limited code...
Grtz,
 Signature Rik Wasmus
Lasse Reichstein Nielsen - 24 Jun 2006 16:20 GMT > /^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{2,4}$/i > > Allthough I'm not sure which characters are actually allowed in an > emailadress. That's reason enough *not* to be too specific. A prefectly good looking email like me@example.com will probably be wrong, whereas me+bad@[192.168.10.2] might be correct (for another IP address at least).
> Then again, I don't know where the validation is for, it could be > sufficient. You can't validate an email adress perfectly just by looking at it (i.e., check that it is an email address that will receive mail for the correct person). The only one who knows that is the person writing it. It's *really* annoying to have a correct email address rejected by an overzealous validator, so it's better to err on the side of caution. For that, I prefer just checking for "@" and "." in that order, which will at least catch really bad errors.
As for the specification, mail addresses were specified in RFC 822, and probably revised in RFC 2822. Here is someone who has tried interpreting it: <URL:http://www.cs.tut.fi/~jkorpela/rfc/822addr.html>
/L
 Signature Lasse Reichstein Nielsen - lrn@hotpop.com DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'
Rik - 24 Jun 2006 16:41 GMT >> /^[0-9a-z+_\.\-]+@([0-9a-z+_\.\-]+\.)+[a-z]{2,4}$/i >> [quoted text clipped - 16 lines] > side of caution. For that, I prefer just checking for "@" and "." > in that order, which will at least catch really bad errors. Well, then I'd at least do this: /^[^\.@]+(\.*[^\.@]+)*@[^\.@](\.*[^\.@]+)+$/
Checking for the disallowes starting/ending dots is relativly simple to incorporate. It all depends on where you are going to use the emailadress for I suppose.
Grtz,
 Signature Rik Wasmus
Bart Van der Donck - 24 Jun 2006 19:17 GMT > [...] > [quoted text clipped - 4 lines] > incorporate. > It all depends on where you are going to use the emailadress for I suppose. There's much more to it. If you're planning to write a solid email address validator, I think you should at least scope it down to the RFC you wish to comply with. Nowadays it looks as if RFC2822 would be a good choice.
Javascript and Perl regular expression syntaces are quite similar, maybe the following could be a start:
http://search.cpan.org/~cwest/Email-Address-1.80/lib/Email/Address.pm (=for RFC2822)
 Signature Bart
libsfan01 - 24 Jun 2006 20:05 GMT Sorry but i didnt get much from the solution, im looking for the JS equivalent of php's function: strstr ( $haystack, $needle ) which returns false if the needle is not found in the haystack.
is there something i can use which looks like this:
if (strstr( form.email.value, '@') == FALSE) { return false }
> > [...] > > [quoted text clipped - 15 lines] > http://search.cpan.org/~cwest/Email-Address-1.80/lib/Email/Address.pm > (=for RFC2822) libsfan01 - 24 Jun 2006 20:20 GMT am i right in thinking indexof() is the baby for this
> Sorry but i didnt get much from the solution, im looking for the JS > equivalent of php's function: strstr ( $haystack, $needle ) which [quoted text clipped - 23 lines] > > http://search.cpan.org/~cwest/Email-Address-1.80/lib/Email/Address.pm > > (=for RFC2822) Randy Webb - 24 Jun 2006 21:18 GMT libsfan01 said the following on 6/24/2006 3:20 PM:
> am i right in thinking indexof() is the baby for this "baby for this"? Are you writing JS or delivering babies?
indexof will tell you if a character is in a string but it is a long way from doing what you original asked about.
@.yourHouse
indexOf('@') ==> true indexOf('.') ==> true
@.yourMama ==> Nowhere close to an email address.
 Signature Randy comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/ Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dr John Stockton - 25 Jun 2006 14:46 GMT JRS: In article <1151175951.297793.127190@c74g2000cwc.googlegroups.com> , dated Sat, 24 Jun 2006 12:05:51 remote, seen in news:comp.lang.javascript, libsfan01 <mcyi2mr3@googlemail.com> posted :
>Sorry but i didnt get much from the solution, im looking for the JS >equivalent of php's function: strstr ( $haystack, $needle ) which [quoted text clipped - 3 lines] > >if (strstr( form.email.value, '@') == FALSE) { return false } There is. However, using == false or its equivalent is a sign of fundamental inadequacy of understanding in most languages. In javascript, so is (in almost all cases, including this one) attempting string validation without using any RegExps.
Please read the newsgroup FAQ on the proper formatting of responses.
When validating E-mail addresses, where the necessary formats may change during the life of the code, one should do no more than reject any which it is thought cannot possibly be right - blank, for example. It may be well only to warn if an address seems incorrect, allowing it to be used if the user agrees.
 Signature © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 © <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Randy Webb - 25 Jun 2006 23:50 GMT Dr John Stockton said the following on 6/25/2006 9:46 AM:
> JRS: In article <1151175951.297793.127190@c74g2000cwc.googlegroups.com> > , dated Sat, 24 Jun 2006 12:05:51 remote, seen in [quoted text clipped - 9 lines] > There is. However, using == false or its equivalent is a sign of > fundamental inadequacy of understanding in most languages. More of this debate? And you missed telling him that FALSE != false
 Signature Randy comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/ Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Rik - 25 Jun 2006 00:19 GMT > Javascript and Perl regular expression syntaces are quite similar, > maybe the following could be a start: > > http://search.cpan.org/~cwest/Email-Address-1.80/lib/Email/Address.pm > (=for RFC2822) I'm actually more at home in PERL/PHP then javascript :-). I'd have to look at tha package itself, maybe tomorrow. Then again, I saw Friedl's solution for complete compliace with every possible valid emailadress, and was in awe. I'm instantly giving up trying to get anywhere near his solution. The damned thing is, when I try to find it again, I only get references to his book, and my browser history has been cleaned in the mean while....
Then again, maybe his book is a good purchase, really mastering regular expressions is usually a time-saver in the first place, and rewriting them when the reuirements change much simpler then huge quantaties of custom code.
Grtz,
 Signature Rik Wasmus
John W. Kennedy - 24 Jun 2006 22:44 GMT > You can't validate an email adress perfectly just by looking at it > (i.e., check that it is an email address that will receive mail [quoted text clipped - 3 lines] > side of caution. For that, I prefer just checking for "@" and "." > in that order, which will at least catch really bad errors. Even that will yield false positives in the trivial cases of mail between two users on the same Unix system ("john") or two systems in the same domain ("jane@payroll" -- the ability to abbreviate "payroll.world-wide-wickets.com" to "payroll" in this way is the historic reason that domains go right-to-left in the first place.)
 Signature John W. Kennedy "The blind rulers of Logres Nourished the land on a fallacy of rational virtue." -- Charles Williams. "Taliessin through Logres: Prelude"
pegasusflightresources@gmail.com - 25 Jun 2006 12:34 GMT > Hi all! > [quoted text clipped - 4 lines] > > Marc Dear sir, Instead of all of this, you could just add a function:
function checkEmail() { var problem; if ((document.form.email.value.indexOf("@" != -1) && (document.form.email.value.indexOf("." != -1) { problem = 0; } else { problem = 1; } }
And then have the function return the value of problem. From there, you can just have an alert popup if problem=1, and otherwise have it run the form submission.
I have the honor to remain your most humble and Ob't Sv't in our war against the King.
-- Patrick Reilly 1st Coy. Colonel Seth Warner's Regiment
Evertjan. - 25 Jun 2006 16:36 GMT pegasusflightresources@gmail.com wrote on 25 jun 2006 in comp.lang.javascript:
> (document.form.email.value.indexOf("." != -1) "." != -1
seems always to be true.
Does true have an index in a string?
 Signature Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
Randy Webb - 25 Jun 2006 18:06 GMT pegasusflightresources@gmail.com said the following on 6/25/2006 7:34 AM:
>> Hi all! >> [quoted text clipped - 25 lines] > you can just have an alert popup if problem=1, and otherwise have it > run the form submission. And you run into the potential scenario I gave:
@.nowhere
Will return valid from your function even though it isn't even close to a valid email address.
The only way to know an email address is valid for use is to email it and receive a positive reply. Otherwise, it is just a semi-educated guess.
 Signature Randy comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/ Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
pegasusflightresources@gmail.com - 26 Jun 2006 14:38 GMT > pegasusflightresources@gmail.com said the following on 6/25/2006 7:34 AM: > >> Hi all! [quoted text clipped - 42 lines] > Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/ > Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ Dear sir, You could just simply add a couple of lines using charAt() determining where the @ is placed and where the "." is placed, and you could even change the "." indexOf() to lastIndexOf(), therefore eliminating the possibility that a person has a dot in their username, instead of their e-mail domain. As long as you can determine that the "@" and "." are not right next to each other (using charAt() and some easy subtraction), you can avoid the proposed problem with this. The result would be something like:
function checkEmail() { var problem; if ((document.form.email.value.indexOf("@") != -1) && (document.form.email.value.indexOf(".") != -1) && (document.form.email.value.indexOf("@") != 0) && (document.form.email.value.lastIndexOf(".") > document.form.email.value.indexOf("@") + 1) && (document.form.email.value.lastIndexOf(".") < document.form.email.length) && (document.form.email.value.indexOf(" ") == -1) { problem = 0; } else { problem = 1; } }
And again, as I said, just have something that alerts of an error if problem returns 1, and if it returns 0 then it continues to process the e-mail address.
I have the honor to remain your most humble and Ob't Sv't in our war against the King.
-- Patrick Reilly 1st Coy. Colonel Seth Warner's Regiment
pegasusflightresources@gmail.com - 28 Jun 2006 19:40 GMT > > pegasusflightresources@gmail.com said the following on 6/25/2006 7:34 AM: > > >> Hi all! [quoted text clipped - 84 lines] > 1st Coy. > Colonel Seth Warner's Regiment Thank you Dr. Stockton for your reminder, as I did forget the parenthesis to end the if qualifying expression. Corrected, the script would look like this:
function checkEmail() { var problem; if ((document.form.email.value.indexOf("@") != -1) && (document.form.email.value.indexOf(".") != -1) && (document.form.email.value.indexOf("@") != 0) && (document.form.email.value.lastIndexOf(".") > document.form.email.value.indexOf("@") + 1) && (document.form.email.value.lastIndexOf(".") < document.form.email.length) && (document.form.email.value.indexOf(" ") == -1)) { problem = 0; } else { problem = 1; } }
And by the way, it is tested, and works perfectly well, I just misstyped it.
I have the honor to remain your most humble and Ob't Sv't in our war against the King.
-- Patrick Reilly 1st Coy. Colonel Seth Warner's Regiment
Evertjan. - 28 Jun 2006 20:38 GMT pegasusflightresources@gmail.com wrote on 28 jun 2006 in comp.lang.javascript:
> function checkEmail() > { [quoted text clipped - 15 lines] > } > } If ...indexOf("@") is not -1 and not 0 it must be >0
Starting a adres with a . seems strange, so also >0
The function seems not very useful, since the local variable "problem" is not used inside the function and not available outside it.
try:
function checkEmail() { var tmp = document.form.email var tmpval = tmp.value if ( (tmpval.indexOf(".") > 0 ) && (tmpval.indexOf("@") > 0 ) && (tmpval.lastIndexOf(".") > tmpval.indexOf("@") + 1) && (tmpval.lastIndexOf(".") < tmp.length) && (tmpval.indexOf(" ") == -1)) return 0; return 1; }
problem = checkEmail();
 Signature Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
pegasusflightresources@gmail.com - 29 Jun 2006 17:33 GMT > pegasusflightresources@gmail.com wrote on 28 jun 2006 in > comp.lang.javascript: [quoted text clipped - 46 lines] > The Netherlands. > (Please change the x'es to dots in my emailaddress) Dear sir, True, but I was trying to avoid using a sort of parameter or return value. Please don't ask why.
I have the honor to remain your most humble and Ob't Sv't in our war against the King.
-- Patrick Reilly 1st Coy. Colonel Seth Warner's Regiment
Dr John Stockton - 29 Jun 2006 14:55 GMT JRS: In article <1151520010.648569.76990@y41g2000cwy.googlegroups.com>, dated Wed, 28 Jun 2006 11:40:10 remote, seen in news:comp.lang.javascript, pegasusflightresources@gmail.com <pegasusflig htresources@gmail.com> posted :
>Lines: 128
>Thank you Dr. Stockton for your reminder, You are responding to yourself, and in a branch of the thread to which I have not contributed.
> as I did forget the >parenthesis to end the if qualifying expression. Corrected, the script [quoted text clipped - 22 lines] >And by the way, it is tested, and works perfectly well, I just >misstyped it. It cannot "work perfectly well", as it has no external effect; it returns undefined.
It is naive to believe that code which is re-typed will be free of new error. If you cannot copy'n'paste tested code, then either don't post "complete" code or mark it as re-typed. Moreover, YSCIB.
Read the newsgroup FAQ (previously cited in this thread) and/or the references below on the appropriate formatting of Usenet responses. In particular, superfluous material and signatures should not be quoted.
You have already been given, in this thread, a two-line version of your algorithm, with proper input and output; and you have also been given a better and similarly short algorithm using a RegExp. Why, then, do you think your own contribution can be of any value?
>-- DSS.
 Signature © John Stockton, Surrey, UK. yyww merlyn demon co uk Turnpike v4.00 MIME © Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A. Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News. No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
pegasusflightresources@gmail.com - 30 Jun 2006 14:39 GMT > JRS: In article <1151520010.648569.76990@y41g2000cwy.googlegroups.com>, > dated Wed, 28 Jun 2006 11:40:10 remote, seen in [quoted text clipped - 7 lines] > You are responding to yourself, and in a branch of the thread to which I > have not contributed. You have contributed, my dear sir, because you have corrected my mistake in typing.
> > as I did forget the > >parenthesis to end the if qualifying expression. Corrected, the script [quoted text clipped - 25 lines] > It cannot "work perfectly well", as it has no external effect; it > returns undefined. Now this, I explained previously. The person that asked for this script, I said, would have to add a piece of code that would give an alert if problem was equal to 1. If you wish, I could just change the part with "problem = 0" and "problem = 1" to: alert("This form was filled out correctly!");
and: alert("This was incorrectly filled out, please try again."); return false;
> It is naive to believe that code which is re-typed will be free of new > error. If you cannot copy'n'paste tested code, then either don't post [quoted text clipped - 8 lines] > better and similarly short algorithm using a RegExp. Why, then, do you > think your own contribution can be of any value? Because any contribution can be of value, as one may not understand the basis of the "short algorithm using a RegExp", and so they would be more influenced to use the longer version because they understand it better and would be able to modify it more easily than modifying something they do not understand (fully). This could happen, so a simplified version may be of consideration.
> >-- > DSS. [quoted text clipped - 4 lines] > Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News. > No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News. I have the honor to remain your most humble and Ob't Sv't in our war against the King.
-- Patrick Reilly 1st Coy. Colonel Seth Warner's Regiment
Dr John Stockton - 25 Jun 2006 20:15 GMT JRS: In article <1151235254.755876.213350@m73g2000cwd.googlegroups.com> , dated Sun, 25 Jun 2006 04:34:14 remote, seen in news:comp.lang.javascript, pegasusflightresources@gmail.com <pegasusflig htresources@gmail.com> posted :
>function checkEmail() >{ >var problem; >if ((document.form.email.value.indexOf("@" != -1) && Clearly untested, or else the missing parentheses would have been detected.
>(document.form.email.value.indexOf("." != -1) > { [quoted text clipped - 5 lines] > } >} For true/false values in legible programming one should use true and false, not 0 and 1.
Your function sets an internal variable problem, and returns undefined.
Code should be indented to show logical structure; a unit of one character is inadequate; and one should indent for more than just 'if' statements.
The function that, for your algorithm, you should have written would go more like
function checkEmail(Ctrl) { var Tmp = Ctrl.value return (Tmp.indexOf("@") != -1) && (Tmp.indexOf(".") != -1) }
Your algorithm, however, is weak, it accepts ".@" and "@." and "@.@" for example. One can do much better with a simple RegExp, as in the first response (by BVdD).
Yours is a rather '70s style of programming ...
>I have the honor to remain your most humble and Ob't Sv't in our war >against the King. ... and that's 1770's.
 Signature © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 © <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
|
|
|