Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Valid email OR empty

Last post 11-23-2008, 5:32 PM by Aussie Susan. 5 replies.
Sort Posts: Previous Next
  •  11-20-2008, 6:11 PM 48590

    Valid email OR empty

    I have found a great deal of advice here in the forum, and "borrowed" a good many Regex codes from you all. Thank you very much. I'm learning how Regex works, but in this case I am pressed for time as this project is going live very soon.

     There are a good deal of Regex to validate an email address. I found a great one to constrain users to a predefined suffix, but I can't get it to work allowing the user the option to enter nothing.

     The code I'm using is          ([a-zA-Z0-9_\-\.]+)(@email.com)

     What can I do to allow an empty entry?

     

    Thanks so much for your help, 

  •  11-20-2008, 6:19 PM 48591 in reply to 48590

    Re: Valid email OR empty

    The simple answer is to make the whole thing optional as in:

    (([a-zA-Z0-9_\-\.]+)(@email.com))?

    but this is probably NOT what you want because it will find the gap between each and every character in the text you are scanning. (Using the text of "hello" returns 6 matches, each of the null string).

    If the email address is the only thing in the text, then either a coded check (length(string) = 0) or the above suggestion will work. If you are wating to locate an email address within some other text then you will need to tell us how we can detect when there is a missing email.

    By the way, the '.' character does not need to be escaped within a character class.

    I've found that, when I'm pressed for time, it sometimes pays to stop and think clearly about what I'm doing in the broarder context which often puts things into perspective, meaning that I end up with the solution faster than hammering away at the wrong problem.

    Susan

  •  11-20-2008, 6:38 PM 48592 in reply to 48591

    Re: Valid email OR empty

    If for nothing else than the speed at which you answered my post, thank you very much.

    Correct me if I'm wrong, but does this solution allow for any entry at all? The code I was using allowed the user to enter anything at all, provided the suffix is @email.com I don't really care if they use illegal characters, or even if the address is otherwise invalid. I just need to ensure they use the correct suffix. The wrench in my plans was that my client also wants to allow the option for no email address!

    Oh, and what you suggested about "hammering away at the wrong problem"... That pretty much describes my day.

     

    Thanks again!

  •  11-20-2008, 8:57 PM 48597 in reply to 48592

    Re: Valid email OR empty

    As regards the speed of my response, I happened to see your question come up just as I finished commenting on another one - doesn;t happen often as you can see by the delay in my making this response!!!!

    If that is what you want then you could use

    @email\.com$

    which will only check that the last10 characters are the wanted ones. Anything else before the last @ will be ingored and therefore 'accepted'

    If you want to allow for a blank response within the regex (as opposed to a check in code which I would think is better) then:

    (@email\.com)?$

    or

    ^.*?@email.com$|^$

    or

    ^(.*?@email.com)?$

    or some other similar construct would do.

    Susan

     

  •  11-21-2008, 11:19 AM 48629 in reply to 48597

    Re: Valid email OR empty

    Success!

    Ok, so it's a partial success, but in this case it's fine for what I need to bring to the client. It allows for no entry at all and constrains the suffix as required. It also allows no entry before the suffix, which might create room for error, but that's not my problem. This will please the client.

    Thanks very much. 

  •  11-23-2008, 5:32 PM 48721 in reply to 48629

    Re: Valid email OR empty

    If this does come back to haunt you, it sounds like your solution uses one of my suggestions that just checks for the '@email.com' part at the end of a line.

    If you use one that includes the '^' at the start of the pattern, then it should require something before the '@email' part or nothing at all.

    Susan

View as RSS news feed in XML