Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > heads or tails of email address parser

Reply
Thread Tools

heads or tails of email address parser

 
 
chadlupkes
Guest
Posts: n/a
 
      02-18-2007
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;
}
<?}?>
}

 
Reply With Quote
 
 
 
 
-Lost
Guest
Posts: n/a
 
      02-18-2007
"chadlupkes" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
pcx99
Guest
Posts: n/a
 
      02-18-2007
-Lost wrote:
> "chadlupkes" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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
 
Reply With Quote
 
Kimmo Laine
Guest
Posts: n/a
 
      02-18-2007
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ö
http://www.velocityreviews.com/forums/(E-Mail Removed) | Gedoon-S @ IRCnet | rot13((E-Mail Removed))
 
Reply With Quote
 
-Lost
Guest
Posts: n/a
 
      02-18-2007
"Kimmo Laine" <(E-Mail Removed)> wrote in message
news:er8us3$ktc$(E-Mail Removed)...

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


 
Reply With Quote
 
Kimmo Laine
Guest
Posts: n/a
 
      02-18-2007
-Lost kirjoitti:
> "Kimmo Laine" <(E-Mail Removed)> wrote in message
> news:er8us3$ktc$(E-Mail Removed)...
>
>> "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ö
(E-Mail Removed) | Gedoon-S @ IRCnet | rot13((E-Mail Removed))
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      02-18-2007
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.

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      02-18-2007
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.

 
Reply With Quote
 
chadlupkes
Guest
Posts: n/a
 
      02-18-2007
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" <(E-Mail Removed)>
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.


 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      02-18-2007
In comp.lang.javascript message <(E-Mail Removed)
glegroups.com>, Sat, 17 Feb 2007 22:11:21, chadlupkes
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Heads-up guys: free geographic fax to email Graham. UK VOIP 12 10-21-2010 09:55 AM
My own email address as a SIP address? UK VOIP 24 01-10-2006 07:22 PM
Heads or Tails Plato Computer Support 42 12-17-2004 08:56 PM
Cat O Nine Tails defective dvd? Mkebeliev DVD Video 1 08-19-2004 02:26 PM
XMLparser: Difference between parser.setErrorHandler() vs. parser.setContentHandler() Bernd Oninger Java 0 06-09-2004 01:26 AM



Advertisments