Regex or InString Help - Using ASP, Searching for Instance of Only 2 Quotes " in an Email Address



  • Dear Daily WTF,

    I’m needing to ascertain the following from an email address that will be submitted via an online form using ASP to process:

    1. Does it have exactly 2 Quotation marks (" and another ") located anywhere left of the @ symbol?
    2. Does it have at least 1 period located anywhere to the right of the @ symbol?

    I started off trying to find a clever way of using the Instring function to find the occurrences of (“) only twice in the string, but I got links all over the Net pointing to:


    Perhaps a WTF in and of itself…I tried the code (yes, after correcting one of his incorrect variable names, etc)  but could not get it to work. Simply looking at the logic looked off to me…

    I’m new to coding and only know HTML, and enough ASP to barely scrape by.

    I was thinking a simple RegEx might do the trick.

    If it’s possible, I’d like LoneProgrammer to take a look at this post. He seems pretty knowledgeable when it comes to Regexes.

    Thanks,

    P.S. Please, no WTFs!!!
    Keep it Good, Simple, Fast Logic.


  • Discourse touched me in a no-no place

    @Caution-MyCode_LooksAndActsLike_PeeWeeHerman said:

    I started off trying to find a clever way

    [...]

    I was thinking a simple RegEx might do the trick.

    Clever normally isn't good.

    Email addresses and regex's have come up here before (search for [email regex],) and they're generally thought of as a bad way of (solely) validating emails.

    A simple way is

    1) Is there an @ sign?

    2) Does the stuff to the right of it accept emails? (Is there an MX record for it?)

    3) Send a token/verification email to the full address.

    4) Wait for the token to come back.

    Stop when any of these steps fail. Do not use the email address until (4) happens.



  • I don't do Regex or ASP, but this is pretty easy with wildcards...this will use SQL syntax.  Should be easy to translate to ASP

     

    1. Does it have exactly 2 Quotation marks (" and another ") located anywhere left of the @ symbol?

    like  '%"%"%@%' and not like '%"%"%"@%'

    2. Does it have at least 1 period located anywhere to the right of the @ symbol?

      like '@%.%'



  • @Salami said:

    I don't do Regex or ASP, but this is pretty easy with wildcards...this will use SQL syntax.  Should be easy to translate to ASP

     

    1. Does it have exactly 2 Quotation marks (" and another ") located anywhere left of the @ symbol?

    like  '%"%"%@%' and not like '%"%"%"@%'

    2. Does it have at least 1 period located anywhere to the right of the @ symbol?

      like '@%.%'

     

    'a"b"c"d@e' would (wrongly) match here.

    I think you want '..%@%' in the pattern here. And you'd have to check for two '@'. And ...

    Furthermore, if you don't check for the length before doing these, good luck verifying a multi-KB string consisting of nothing but '@' and '"' within an acceptable timeframe ... depending on your matching engine you might end up with rather bad backtracking.



  •  simplest way is just to try to send a test email.  don't bother doing any verification other than the @ sign.



  •  @Caution-MyCode_LooksAndActsLike_PeeWeeHerman said:

    Dear Daily WTF,

    I’m needing to ascertain the following from an email address that will be submitted via an online form using ASP to process:

    1. Does it have exactly 2 Quotation marks (" and another ") located anywhere left of the @ symbol?
    2. Does it have at least 1 period located anywhere to the right of the @ symbol?

    If those are your only criteria, then this regex might work...

    [^"@]*"[^"@]*"[^"@]*@.*\.

    It won't guarantee that there's not another @ in the address, or that there's anything inside the quotes, or that there's nothing outside...but you didn't ask for that.



  • @cHao said:

     @Caution-MyCode_LooksAndActsLike_PeeWeeHerman said:

    Dear Daily WTF,

    I’m needing to ascertain the following from an email address that will be submitted via an online form using ASP to process:

    1. Does it have exactly 2 Quotation marks (" and another ") located anywhere left of the @ symbol?
    2. Does it have at least 1 period located anywhere to the right of the @ symbol?

    If those are your only criteria, then this regex might work...

    [^"@]"[^"@]"[^"@]@..

    Those were not the only two criteria.  You missed the first: it's an email address.

    ^"[^"@]*"@[^@"]*\.[^@"]*$

    According to both RFC 821 and RFC 2821, an email address cannot have unquoted double quotes in it.  If an email address is using double quotes as quoting characters, they MUST be the first and last characters of the LHS (left-hand side).  I am assuming that the OP is not attempting to handle wacked-out email addresses such as look\"ma-my$email\"address.has%funny@chars.example.com

    I can get you the exact paragraph cites if you need it.

    Also, unless there's another check to ensure just one @ character, you need to do that, too.

    Disclaimer: I don't ASP.  I do regexes in Perl, grep, vim, sed, awk, and many other languages on unix systems.



  • If you were using ASP.Net you could use the System.Net.Mail.MailAddress object to test the validity of the address.
    The object throws a FormatException if it doesn't think the string is a valid email address.

    MailAddress ma = new MailAddress("user@unknown.com");   // works
    MailAddress ma2 = new MailAddress("invalid.email.address");  // throws (FormatException) The specified string is not in the form required for an e-mail address.
    

Log in to reply