Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   heads or tails of email address parser (http://www.velocityreviews.com/forums/t929814-heads-or-tails-of-email-address-parser.html)

chadlupkes 02-18-2007 06:11 AM

heads or tails of email address parser
 
I have this code from someone else, and I'm trying to make heads or
tails of it because IE doesn't like it. Can anyone help? Or does
anyone have a better idea?

/* parse the email to check for valid form */
function parseemail(str)
{
str = trim(str);
<?if(preg_match("/MSIE 5.0;/", $_SERVER['HTTP_USER_AGENT'])) // this
is IE 5.0
{?>
if(str.search(/\w+@[\w\W]+\.[a-z]{3}/) == -1)
{
window.alert("Please enter a valid email address");
return true;
}
<?}else{ // not IE 5.0
?>
if(str.search(/^[\w\-\.]+[@][\w\-]+(?:\.[\w\-]+)+$/) == -1) // ||
str.search(/.+?\@([\w.-]+?\s)/) == -1)
{
window.alert("Please enter a valid email address");
return true;
}
<?}?>
}


-Lost 02-18-2007 06:36 AM

Re: heads or tails of email address parser
 
"chadlupkes" <chadlupkes@gmail.com> wrote in message
news:1171779081.479141.299610@q2g2000cwa.googlegro ups.com...
>I have this code from someone else, and I'm trying to make heads or
> tails of it because IE doesn't like it. Can anyone help? Or does
> anyone have a better idea?
>
> /* parse the email to check for valid form */
> function parseemail(str)
> {
> str = trim(str);
> <?if(preg_match("/MSIE 5.0;/", $_SERVER['HTTP_USER_AGENT'])) // this
> is IE 5.0
> {?>
> if(str.search(/\w+@[\w\W]+\.[a-z]{3}/) == -1)
> {
> window.alert("Please enter a valid email address");
> return true;
> }
> <?}else{ // not IE 5.0
> ?>
> if(str.search(/^[\w\-\.]+[@][\w\-]+(?:\.[\w\-]+)+$/) == -1) // ||
> str.search(/.+?\@([\w.-]+?\s)/) == -1)
> {
> window.alert("Please enter a valid email address");
> return true;
> }
> <?}?>
> }


I swear this looks like PHP and JavaScript.

-Lost



pcx99 02-18-2007 07:08 AM

Re: heads or tails of email address parser
 
-Lost wrote:
> "chadlupkes" <chadlupkes@gmail.com> wrote in message
> news:1171779081.479141.299610@q2g2000cwa.googlegro ups.com...
>> I have this code from someone else, and I'm trying to make heads or
>> tails of it because IE doesn't like it. Can anyone help? Or does
>> anyone have a better idea?
>>
>> /* parse the email to check for valid form */
>> function parseemail(str)
>> {
>> str = trim(str);
>> <?if(preg_match("/MSIE 5.0;/", $_SERVER['HTTP_USER_AGENT'])) // this
>> is IE 5.0
>> {?>
>> if(str.search(/\w+@[\w\W]+\.[a-z]{3}/) == -1)
>> {
>> window.alert("Please enter a valid email address");
>> return true;
>> }
>> <?}else{ // not IE 5.0
>> ?>
>> if(str.search(/^[\w\-\.]+[@][\w\-]+(?:\.[\w\-]+)+$/) == -1) // ||
>> str.search(/.+?\@([\w.-]+?\s)/) == -1)
>> {
>> window.alert("Please enter a valid email address");
>> return true;
>> }
>> <?}?>
>> }

>
> I swear this looks like PHP and JavaScript.
>
> -Lost
>



You swear right! It sends one block of code to the browser if the
client is MSIE version 5 and another for all other versions. Probably
to work around some bug or another in IE. In short, php writing
javascript, one of my favorite things about net programming. It doesn't
go quite as far as php writing javascript writing html creating styles
with a little dynamically created javascript through an eval or two but
it's still pretty fun.

This script will need to be placed on a server with php to work
properly. It can be altered to function without the need for php.


function parseemail(str) {
str = trim(str);
if (navigator.userAgent.indexOf('MSIE 5.0')>0) {
if(str.search(/\w+@[\w\W]+\.[a-z]{3}/) == -1) {
window.alert("Please enter a valid email address");
return true;
} else {
if(str.search(/^[\w\-\.]+[@][\w\-]+(?:\.[\w\-]+)+$/) == -1) {
window.alert("Please enter a valid email address");
return true;
}
}
}
}

I didn't test the function so it will probably implode, or explode but
at least it gets a little closer to being liked by explorer.


--
http://www.hunlock.com -- Musings in Javascript, CSS.
$FA

Kimmo Laine 02-18-2007 07:22 AM

Re: heads or tails of email address parser
 
chadlupkes kirjoitti:
> I have this code from someone else, and I'm trying to make heads or
> tails of it because IE doesn't like it. Can anyone help? Or does
> anyone have a better idea?


Starts with a javascript function definition:

> /* parse the email to check for valid form */
> function parseemail(str)
> {
> str = trim(str);


Trims whitespace from the input

> <?if(preg_match("/MSIE 5.0;/", $_SERVER['HTTP_USER_AGENT'])) // this
> is IE 5.0
> {?>


Enter php. If the useragent is IE 5 it prints this block of js:

> if(str.search(/\w+@[\w\W]+\.[a-z]{3}/) == -1)
> {


If the reg exp doesn't match, the js pops an alertbox.

> window.alert("Please enter a valid email address");
> return true;
> }
> <?}else{ // not IE 5.0
> ?>


It's not IE, so it prints this js:

> if(str.search(/^[\w\-\.]+[@][\w\-]+(?:\.[\w\-]+)+$/) == -1) // ||
> str.search(/.+?\@([\w.-]+?\s)/) == -1)
> {
> window.alert("Please enter a valid email address");
> return true;
> }


Both blocks apparently try to validate an email address with a
half-assed regular expression. The first one in particular assumes that
all domains are in format of domain.xyz where the xyz is exactly three
letters, no more or less, this is quite wrong assumption.
something.co.uk, something.fi and something.info might all be valid
domains but do not pass the test.

> <?}?>
> }
>


A complete rewrite couldn't make it worse. I find it difficult to
believe that the javascript engines on different browsers would be so
horribly iincomptaible that you'd need to write a separate regexp for
it, but then again with IE nothing suprises me.

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)

-Lost 02-18-2007 09:38 AM

Re: heads or tails of email address parser
 
"Kimmo Laine" <spam@outolempi.net> wrote in message
news:er8us3$ktc$1@nyytiset.pp.htv.fi...

> "En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö


I am horrible at Finnish and the best translation I got is something about a "sinister
man" and an "apple."

What is the literal translation in English, preferably, please.

-Lost



Kimmo Laine 02-18-2007 10:22 AM

Re: heads or tails of email address parser
 
-Lost kirjoitti:
> "Kimmo Laine" <spam@outolempi.net> wrote in message
> news:er8us3$ktc$1@nyytiset.pp.htv.fi...
>
>> "En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö

>
> I am horrible at Finnish and the best translation I got is something about a "sinister
> man" and an "apple."
>
> What is the literal translation in English, preferably, please.


"En ole paha ihminen, mutta omenat ovat elinkeinoni." =
"I am not a bad person, but apples are my trade."

And the explanation to where this expression originates and why I
consider it funny is rather lengthy, short version: it was said by a
Finnish Big Brother 2005 contestant Perttu.

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)

Richard Cornford 02-18-2007 01:04 PM

Re: heads or tails of email address parser
 
Kimmo Laine wrote:
> chadlupkes kirjoitti:

<snip>
>> str = trim(str);

>
> Trims whitespace from the input


Assuming a congaing scope defines a function called - trim - and trimming
white space is what that function does (likely but not certain).

>> <?if(preg_match("/MSIE 5.0;/", $_SERVER['HTTP_USER_AGENT']))
>> // this is IE 5.0
>> {?>

>
> Enter php. If the useragent is IE 5 it prints this block of js:


Not 'if the user agent is' but 'if the user agent header contains the
character sequence'.

<snip>
> A complete rewrite couldn't make it worse.


It is irrational as it is.

> I find it difficult to believe that the javascript engines on
> different browsers would be so horribly iincomptaible that you'd
> need to write a separate regexp for it,


They re not. The standard for the language provides a minimum syntax for
regular expressions, and then allows implementations to extend it. You
achieve cross-browser compatibility by using the specified syntax and
keep the extensions for use only when you know the browser environment
with certainty. (that just leaves one or two very minor bugs to be
avoided).

> but then again with IE nothing suprises me.


In this context any fault to be attributed goes to the author of the
code. IE is innocent here.

Richard.


Richard Cornford 02-18-2007 01:04 PM

Re: heads or tails of email address parser
 
pcx99 wrote:
> -Lost wrote:
>> chadlupkes wrote:

<snip>
>>> <?if(preg_match("/MSIE 5.0;/", $_SERVER['HTTP_USER_AGENT']))
>>> // this is IE 5.0
>>> {?>

<snip>
>> I swear this looks like PHP and JavaScript.

<snip>
> You swear right! It sends one block of code to the browser
> if the client is MSIE version 5 and another for all other
> versions.


It sends one block of code if the User Agent header contains the
character sequence "MSIE 5.0" and the other if it does not. That has
nothing to do with whether the browser is IE 5 or not, as the User Agent
header is not specified as being a source of information and many
browsers are known to spoof IE's UA headers (and so some will spoof IE 5
directly, or include the character sequence "MSIE 5.0").

> Probably to work around some bug or another in IE.


It is an irrational attempt to compensate for the level of regular
expression support. It suffers from assuming non-standard regular
expression syntax is available in all browsers that are not IE 5 (or
spoofing IE 5 in some way).

<snip>
> This script will need to be placed on a server with php to work
> properly. It can be altered to function without the need for php.
>
>
> function parseemail(str) {
> str = trim(str);
> if (navigator.userAgent.indexOf('MSIE 5.0')>0) {

^^
For equivalence with the irrational PHP above that should be; greater or
equal to 0, or greater than -1.

> if(str.search(/\w+@[\w\W]+\.[a-z]{3}/) == -1) {
> window.alert("Please enter a valid email address");
> return true;
> } else {
> if(str.search(/^[\w\-\.]+[@][\w\-]+(?:\.[\w\-]+)+$/) == -1) {
> window.alert("Please enter a valid email address");
> return true;
> }
> }
> }
>}
>
> I didn't test the function so it will probably implode, or
> explode but at least it gets a little closer to being liked
> by explorer.

<snip>

The User Agent header (and so the navigator.userAgent string) is not
specified as being a source of information, and so should not be used as
one. There is no relationship, real or implied, between the contents of a
User Agent header and the level of regular expression support on a
browser. Regular expression support in JScript is independent of the
browser version. If the first regular expression is ever acceptable there
is no reason why it should not always be acceptable. Neither regular
expression seems to be a discriminator of a "valid email address" (though
the first is much worse than the second).

A bad idea moved from PHP to the client is not a step forward.

Richard.


chadlupkes 02-18-2007 04:19 PM

Re: heads or tails of email address parser
 
Good discussion.

There are a few other options for validating an email address that
I've found. Here are some links:

http://www.smartwebby.com/DHTML/email_validation.asp

http://www.4guysfromrolla.com/webtech/091998-1.shtml

http://homepage.ntlworld.com/kayseycarvey/jss3p7.html

http://javascript.about.com/od/compl...ts/a/email.htm

http://www.w3schools.com/js/js_form_validation.asp

And there's more:

http://www.google.com/search?hl=en&q...ess+javascript

Thanks for the input! I think I'll go with something a little more
mainstream.

Chad

On Feb 18, 5:04 am, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
> pcx99 wrote:
> > -Lost wrote:
> >> chadlupkes wrote:

> <snip>
> >>> <?if(preg_match("/MSIE 5.0;/", $_SERVER['HTTP_USER_AGENT']))
> >>> // this is IE 5.0
> >>> {?>

> <snip>
> >> I swear this looks like PHP and JavaScript.

> <snip>
> > You swear right! It sends one block of code to the browser
> > if the client is MSIE version 5 and another for all other
> > versions.

>
> It sends one block of code if the User Agent header contains the
> character sequence "MSIE 5.0" and the other if it does not. That has
> nothing to do with whether the browser is IE 5 or not, as the User Agent
> header is not specified as being a source of information and many
> browsers are known to spoof IE's UA headers (and so some will spoof IE 5
> directly, or include the character sequence "MSIE 5.0").
>
> > Probably to work around some bug or another in IE.

>
> It is an irrational attempt to compensate for the level of regular
> expression support. It suffers from assuming non-standard regular
> expression syntax is available in all browsers that are not IE 5 (or
> spoofing IE 5 in some way).
>
> <snip>> This script will need to be placed on a server with php to work
> > properly. It can be altered to function without the need for php.

>
> > function parseemail(str) {
> > str = trim(str);
> > if (navigator.userAgent.indexOf('MSIE 5.0')>0) {

>
> ^^
> For equivalence with the irrational PHP above that should be; greater or
> equal to 0, or greater than -1.
>
> > if(str.search(/\w+@[\w\W]+\.[a-z]{3}/) == -1) {
> > window.alert("Please enter a valid email address");
> > return true;
> > } else {
> > if(str.search(/^[\w\-\.]+[@][\w\-]+(?:\.[\w\-]+)+$/) == -1) {
> > window.alert("Please enter a valid email address");
> > return true;
> > }
> > }
> > }
> >}

>
> > I didn't test the function so it will probably implode, or
> > explode but at least it gets a little closer to being liked
> > by explorer.

>
> <snip>
>
> The User Agent header (and so the navigator.userAgent string) is not
> specified as being a source of information, and so should not be used as
> one. There is no relationship, real or implied, between the contents of a
> User Agent header and the level of regular expression support on a
> browser. Regular expression support in JScript is independent of the
> browser version. If the first regular expression is ever acceptable there
> is no reason why it should not always be acceptable. Neither regular
> expression seems to be a discriminator of a "valid email address" (though
> the first is much worse than the second).
>
> A bad idea moved from PHP to the client is not a step forward.
>
> Richard.



Dr J R Stockton 02-18-2007 08:21 PM

Re: heads or tails of email address parser
 
In comp.lang.javascript message <1171779081.479141.299610@q2g2000cwa.goo
glegroups.com>, Sat, 17 Feb 2007 22:11:21, chadlupkes
<chadlupkes@gmail.com> posted:
>I have this code from someone else, and I'm trying to make heads or
>tails of it because IE doesn't like it. Can anyone help? Or does
>anyone have a better idea?


The best thing you can do with that code is to sort the characters into
"alphabetical" order for re-use elsewhere.

See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>

It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<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.


All times are GMT. The time now is 02:08 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57