heads or tails of email address parser

Discussion in 'Javascript' started by chadlupkes, Feb 18, 2007.

  1. chadlupkes

    chadlupkes Guest

    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;
    }
    <?}?>
    }
    chadlupkes, Feb 18, 2007
    #1
    1. Advertising

  2. chadlupkes

    -Lost Guest

    "chadlupkes" <> wrote in message
    news:...
    >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
    -Lost, Feb 18, 2007
    #2
    1. Advertising

  3. chadlupkes

    pcx99 Guest

    -Lost wrote:
    > "chadlupkes" <> wrote in message
    > news:...
    >> 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
    pcx99, Feb 18, 2007
    #3
  4. chadlupkes

    Kimmo Laine Guest

    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ö
    | Gedoon-S @ IRCnet | rot13()
    Kimmo Laine, Feb 18, 2007
    #4
  5. chadlupkes

    -Lost Guest

    "Kimmo Laine" <> wrote in message
    news:er8us3$ktc$...

    > "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
    -Lost, Feb 18, 2007
    #5
  6. chadlupkes

    Kimmo Laine Guest

    -Lost kirjoitti:
    > "Kimmo Laine" <> wrote in message
    > news:er8us3$ktc$...
    >
    >> "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ö
    | Gedoon-S @ IRCnet | rot13()
    Kimmo Laine, Feb 18, 2007
    #6
  7. 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, Feb 18, 2007
    #7
  8. 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.
    Richard Cornford, Feb 18, 2007
    #8
  9. chadlupkes

    chadlupkes Guest

    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

    On Feb 18, 5:04 am, "Richard Cornford" <>
    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.
    chadlupkes, Feb 18, 2007
    #9
  10. In comp.lang.javascript message <
    glegroups.com>, Sat, 17 Feb 2007 22:11:21, chadlupkes
    <> 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.
    Dr J R Stockton, Feb 18, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Vecchi
    Replies:
    6
    Views:
    483
    Ron Vecchi
    Oct 23, 2003
  2. Gandalf
    Replies:
    2
    Views:
    1,586
    Gandalf
    Mar 7, 2004
  3. Heads in the Sand

    , Jul 21, 2007, in forum: Java
    Replies:
    0
    Views:
    296
  4. Anthony Jones
    Replies:
    4
    Views:
    217
    Chris
    Dec 19, 2008
  5. Martin DeMello
    Replies:
    2
    Views:
    152
    James Edward Gray II
    May 27, 2010
Loading...

Share This Page