heads or tails of email address parser

C

chadlupkes

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;
}
<?}?>
}
 
L

-Lost

chadlupkes said:
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
 
P

pcx99

-Lost said:
chadlupkes said:
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.
 
K

Kimmo Laine

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.
 
L

-Lost

"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
 
K

Kimmo Laine

-Lost kirjoitti:
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.
 
R

Richard Cornford

Kimmo said:
chadlupkes kirjoitti:

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'.

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.
 
R

Richard Cornford

pcx99 said:
-Lost said:
chadlupkes wrote:
<?if(preg_match("/MSIE 5.0;/", $_SERVER['HTTP_USER_AGENT']))
// this is IE 5.0
{?>
I swear this looks like PHP and JavaScript.
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).

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.
 
C

chadlupkes

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/completescripts/a/email.htm

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

And there's more:

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

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

Chad

pcx99 said:
-Lost said:
chadlupkes wrote:
<?if(preg_match("/MSIE 5.0;/", $_SERVER['HTTP_USER_AGENT']))
// this is IE 5.0
{?>
I swear this looks like PHP and JavaScript.
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).

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.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
glegroups.com>, Sat, 17 Feb 2007 22:11:21, chadlupkes
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top