• Drewsky (unregistered) in reply to kipthegreat

    LMAO...That's Brillant!

  • Drewsky (unregistered) in reply to kipthegreat

    kipthegreat:
    He should have had each of the functions he calls from the if statements throw an exception.  That way he could catch them instead of letting them return a possibly troublesome "false" value.

    LMAO...That's Brillant!

  • Cybenny (unregistered) in reply to Maurits

    Maurits:
    function is_valid_email(email)
    {    var syntax_ok;
        var at_position = email.indexOf("@");

        if (at_position == -1)
        {    syntax_ok = false;
        }
        else
        {    var local_part = email.substring(0, at_position);
            var domain_part = email.substring(at_position + 1, email.length);

            var local_part_pattern =
                /^[a-zA-Z0-9\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+(\.[a-zA-Z0-9\!\#\$\%\&\'\*\+\-\/\=\?\^\_\`\{\|\}\~]+)*$/;
            var domain_part_pattern =
                /^[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)+$/;

            if (    local_part.match(local_part_pattern)
                && domain_part.match(domain_part_pattern)
            )
            {    syntax_ok = true;
            } else
            {    syntax_ok = false;
            }
        }

        return syntax_ok;
    }

    And if you use some cleaner coding style the next time you want to show your own stuff then your contribution could have been usefull as well:

    <FONT color=#000000><FONT size=2><FONT face="Courier New" size=1>private static readonly Regex local_part_pattern = new Regex(@"^([a-zA-Z0-9_\-\.+]+)$");
    private static readonly Regex domain_part_pattern = new Regex(@"^((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$");
    public bool is_valid_email(string email)
    {   
     int at_position = email.IndexOf("@");
     if (at_position == -1) return false;
     return local_part_pattern.IsMatch(email.Substring(0, at_position)) &&
      domain_part_pattern.IsMatch(email.Substring(at_position + 1));
    }</FONT>
    </FONT></FONT>

    b.t.w., I don't prefer this naming convention but for recognition I didn't change them.

    Gr,

    Cybenny

  • (cs) in reply to Manni

    Here's another e-mail validator I found in a webpage. Strangely enough, it doesn't even get called (alone with several dozen verification functions on that page).

    function isValidEmail(address) {
     if (address != '' && address.search) {
          if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
          else return false;
     }
       else return true;
    }

    Can you see the WTF?

  • Confused (unregistered) in reply to Adam Heath
    Anonymous:
    Use a regex.

    $RFC822PAT = <<'EOF';
    [\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\
    xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xf
    f\n\015()]*)*\)[\040\t]*)*(?:(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\x
    ff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|"[^\\\x80-\xff\n\015
    "]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[\040\t]*(?:\([^\\\x80-\
    xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80
    -\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*
    )*(?:\.[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\
    \\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\
    x80-\xff\n\015()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x8
    0-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|"[^\\\x80-\xff\n
    \015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[\040\t]*(?:\([^\\\x
    80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^
    \x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040
    \t]*)*)*@[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([
    ^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\
    \\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\
    x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-
    \xff\n\015\[\]]|\\[^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()
    ]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\
    x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\04
    0\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\
    n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\
    015()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?!
    [^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\
    ]]|\\[^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\
    x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\01
    5()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*)*|(?:[^(\040)<>@,;:".
    \\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]
    )|"[^\\\x80-\xff\n\015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[^
    ()<>@,;:".\\\[\]\x80-\xff\000-\010\012-\037]*(?:(?:\([^\\\x80-\xff\n\0
    15()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][
    ^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)|"[^\\\x80-\xff\
    n\015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015"]*)*")[^()<>@,;:".\\\[\]\
    x80-\xff\000-\010\012-\037]*)*<[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?
    :(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-
    \xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:@[\040\t]*
    (?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015
    ()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()
    ]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\0
    40)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\
    [^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\
    xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*
    )*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x80
    -\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x
    80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t
    ]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\
    \[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff])
    *\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x
    80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80
    -\xff\n\015()]*)*\)[\040\t]*)*)*(?:,[\040\t]*(?:\([^\\\x80-\xff\n\015(
    )]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\
    \x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*@[\040\t
    ]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\0
    15()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015
    ()]*)*\)[\040\t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(
    \040)<>@,;:".\\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|
    \\[^\x80-\xff])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80
    -\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()
    ]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x
    80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^
    \x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040
    \t]*)*(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".
    \\\[\]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff
    ])*\])[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\
    \x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x
    80-\xff\n\015()]*)*\)[\040\t]*)*)*)*:[\040\t]*(?:\([^\\\x80-\xff\n\015
    ()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\
    \\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*)?(?:[^
    (\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-
    \037\x80-\xff])|"[^\\\x80-\xff\n\015"]*(?:\\[^\x80-\xff][^\\\x80-\xff\
    n\015"]*)*")[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|
    \([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))
    [^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x80-\xff
    \n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\x
    ff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(
    ?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\
    000-\037\x80-\xff])|"[^\\\x80-\xff\n\015"]*(?:\\[^\x80-\xff][^\\\x80-\
    xff\n\015"]*)*")[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\x
    ff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)
    *\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*)*@[\040\t]*(?:\([^\\\x80-\x
    ff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-
    \xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)
    *(?:[^(\040)<>@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\
    ]\000-\037\x80-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff])*\]
    )[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-
    \xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\x
    ff\n\015()]*)*\)[\040\t]*)*(?:\.[\040\t]*(?:\([^\\\x80-\xff\n\015()]*(
    ?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]*(?:\\[^\x80-\xff][^\\\x80
    -\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)*\)[\040\t]*)*(?:[^(\040)<
    >@,;:".\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\[\]\000-\037\x8
    0-\xff])|\[(?:[^\\\x80-\xff\n\015\[\]]|\\[^\x80-\xff])*\])[\040\t]*(?:
    \([^\\\x80-\xff\n\015()]*(?:(?:\\[^\x80-\xff]|\([^\\\x80-\xff\n\015()]
    *(?:\\[^\x80-\xff][^\\\x80-\xff\n\015()]*)*\))[^\\\x80-\xff\n\015()]*)
    *\)[\040\t]*)*)*>)
    EOF

    $RFC822PAT =~ s/\n//g;



    WTF - would anyone care to explain why on earth such a complex regular expression is required?

    Unless I'm very much mistaken then the something like following only lacks tests for:
    • length of the local-part and sub-domain parts (given the engine I'm using doesn't support lookarounds);
    • acceptance of the phrase <local-part @="" domain=""> </local-part>format (given how unlikely it is to be used when offered separate name and email fields);
    • IPv6 (or mixed IPv6/IPv4) domain-literals (given that I appear to have run into a nesting limit)
    • TLD specific rules for sub-domains (given their vomplexity and variety and that unlike the rest of the spec they are prone to change as new TLDs are established):

    Obviously I stand to be corrected.
  • (cs) in reply to Cybenny
    Anonymous:

    Maurits:
    function is_valid_email(email)
    ...
    }

    And if you use some cleaner coding style the next time you want to show your own stuff then your contribution could have been usefull as well:

    <font color="#000000"><font size="2"><font face="Courier New" size="1">private static readonly Regex local_part_pattern = new</font></font></font>

    ...



    Actually, my submission was Javascript, whereas yours appears to be C#.  Apples and oranges and all that.
  • Anonymous Coward (unregistered) in reply to DrJames
    DrJames:
    If I remember correctly VB can short circuit it just does not do this by default.  I believe the operators are: AndAlso and OrElse
    Err, Whiskey Tango Foxtrot, Over?
  • mister r (unregistered) in reply to Anonymous
    Anonymous:

    I like how none of his variables are typed.

    (AndAlso and OrElse are only available in VB.NET, my guess is that this if VB6).



    I dont program in VB so I might be wrong, but doesn't the compiler infer the types? Or do you loose type-safety when not specifying types ?
  • mister r (unregistered) in reply to wildclaw
    wildclaw:
    int Fact(int a){if(a==0)return 1;else return a*Fact(a-1);}

    If a programmer formatted code like the above it would be considered a big WTF, but somehow it becomes acceptable when using regexes.




    Well, in Haskell this is the preferred way or formatting your code:

      fac n = if n == 0 then 1 else n * fac (n-1)

    Or perhaps:

      fac 1 = 1
      fac n = n * fac (n-1)

    For simple functions like factorial I would actually prefer to keep it small.
    So when I see

      int Fact(int a) { if (a==0) return 1; else return a*Fact(a-1); }

    I dont consider to be a big WTF as you call it.

    wildclaw:

    It doesn't help either that todays editors aren't equipped to handle regex syntax. Even regex editors usually don't provide any formatting capabilities and instead focus almost exclusivly on writing the regex instead of making it readable.


    Well I usually use parser combinators rather than regExpr because they integrate better within the language while being equally expressive. Esspecially in languages that allow you to define your own operators this can be a very clean and maintainable solution that skills well.

    Making it so that it fixes the email-addres for example, would just be a little change, because your already operating in your normal environment. I tend to resist the urge to built javascript/html/parsers/sql straight from strings. Make or find a library that embeds it in the environment in a more natural way, catching possible construction errors. This pays off more in a statically typed environment off course.

  • (cs) in reply to Manni
    Anonymous:
    Anonymous:


    I like how none of his variables are typed.

    (AndAlso and OrElse are only available in VB.NET, my guess is that this if VB6).


    I dont program in VB so I might be wrong, but doesn't the compiler infer the types? Or do you loose type-safety when not specifying types ?


    Not exactly, and certainly not in the sense that a language like Haskell does. In VB 6, if you don't declare a variable's type, it is automatically assumed to be of type Variant, which is a polymorphic reference type - roughly equivalent to class Object in Java or Smalltalk, or void pointer in C. Every Variant has added metadata describing the type of the variable it references, which get set whenever the value is assigned. Furthermore, whenever any operator or function is applied to a Variant, the runtime library attempts to coerce the value to a type appropriate to the operation. While Variants are useful in a number of ways, they have considerable overhead when used for simple scalar types, and can lead to type conflicts if it can't find a way to coerce the value suitably.

    Most VB shops will prevent implicit declarations such as these by requiring all source files to include the 'Option Explicit' pragma. Furthermore, VB.Net closes this loophole by forbidding undeclared types entirely - but then, as others have pointed out, VB.Net is a very different language from VB 6.

  • Me (unregistered)

    It would reject a perfectly valid bang-path

  • (cs)

    Many sites don't accept my perfectly valid (RFC822) email address: $^!#^$!^#$!^#$^!$#!^$^!#$^!#$^$#^!$#^!#$^$^!^$#^!$^#!^$#^!$#^!#$^!$#^!#$^#$^!$#^!$#$^$$^!#$^^!^!^#^#!^^#^$$^$#^^#^$^#^!#^$#^!$^@something-obscure.siliconcluster.net

    And those same sites will no doubt accept: [email protected]

    Email validation via anything other than confirmation emails does not work.

  • BrandonPhone (unregistered)
    Comment held for moderation.

Leave a comment on “There's More Than One Way To Validate An Email”

Log In or post as a guest

Replying to comment #:

« Return to Article