Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Location
    Washington State
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question JS: email regexp w/ONLY one period possible before @

    First class assignment on regular expressions. I'm looking for help and teaching, not someone doing the assignment for me (see the code below for some of the work I've already done). I am attempting to do a basic email format validation in JavaScript on a very basic form with fields for name, address, email, etc.

    Requirements:
    1. A period can occur before the @, but not twice in a row -- as in jane.doe@xyz.com but not jane..doe@xyz.com.
    2. Email must start with a letter.
    3. I'm also allowing underscores and hyphens before the @, but I'm not worrying about whether there's only one of each -- I'm just allowing those.
    4. Must end in .com, .net, .org. Nothing else.


    Here's me flailing around:

    Code:
    \A[a-zA-Z](\w|[.]|-){2,}@\w{3,}.(com|net|org)
    \A[a-zA-Z]\w(-|.){2,}@\w{3,}.(com|net|org)
    \A[a-zA-Z](\w|[-]|[.]){2,}@\w{3,}.(com|net|org)
    \A[a-zA-Z](\w|-|.){3,}@\w{3,}.(com|net|org)
    \A[a-zA-Z](\w|-|\.){3,}@\w{3,}.(com|net|org)
    \A[a-zA-Z](\w|-|\.){3,}[^\.{2,}]@\w{3,}.(com|net|org)
    \A[a-zA-Z](\w|-|[^\.{2,}]){3,}@\w{3,}.(com|net|org)
    \A[a-zA-Z]\w{2,}@\w{3,}.(com|net|org)
    \A[a-zA-Z]([\w|\.|\-]{3,}\w{3,})@[a-zA-Z]{3,}.(com|net|org)
    ^(\w+(?:\.[\w-]+)*)@[a-zA-Z]{3,}.(com|net|org)
    I have read extensively on this site and several others and haven't found a solution to the "one and only one period allowed in succession, but not required" problem. I haven't found a successful way to limit periods to only 1 in a row yet still have the rest of the validation work properly.

    I get the symbols in general, but as a newbie can't seem to make them do this one thing that I want them to do. Thanks for any help that's going!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,036
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    There is no need to re-invent the wheel! Try this:-

    if (/^([a-z0-9])(([\-.]|[_]+)?([a-z0-9]+))*(@)([a-z0-9])((([a-z0-9\-\.]+))?)*((.com)|(.net)|(.org))$/i.test(emailAddress.value)) {

    Note that an email address may start with a digit, but if you really insist on the first character being a letter then replace ^([a-z0-9]) by ^([a-z])

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Hmmm...I think that Lorna's needs are even simpler than what Philip gave.

    Code:
    var reEmail = /^[a-z](\.?[\w\-\])*\@([\w\-]+\.)+(com|net|org)$/i;
    No?

    Now, that *does* allow an address of
    a@b.com
    and does not allow
    a.@b.com
    The former being somewhat short but legal. The latter, though, probably being properly rejected (though not called for in the spec, above).

    Basically, that says:
    start with a letter
    accept zero or more occurrences of an optional period followed by letter/digit/dash/underline
    accept @
    accept one or more occurrences of ( (one or more letter/digit/dash/underline) followed by period )
    accept com or net or org

    It *does* allow
    a.-._.-_@b.com

    But that wasn't excluded by the given rules.
    Code:
    EDIT:  Fun!  This forum recognizes a@b.com and a.-._.-_@b.com as legal!
    
    I put this in code tags so it would show them as is here.
    Wonder what it does with a@_.com ???
    Last edited by Old Pedant; 02-23-2010 at 01:31 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,036
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Well, I don't see how your regex is any improvement on mine.

    Mine allows a@b.com, a.c.d@b.com
    but rejects a.@b.com, a..@b.com, a@ .com and a.-._.-_@b.com

    So how is yours significantly different, except slightly shorter by using \w?

    Lorna's thread title says "only one . possible before the @" but she then goes on to say "A period can occur before the @, but not twice in a row". I have interpreted this contradiction to mean two consecutive periods not allowed, as of course a.c.d@b.com is perfectly legal.

    But in any case Lorna does not seem to be impressed by the ability of either of us to meet her needs. She probably thinks we are too old.



    Don't waste your money on ineffective and possibly dangerous pumps, exercises and surgeries.
    Last edited by Philip M; 02-23-2010 at 08:31 AM.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Oh, not better in any sense. Just shorter, that's all. Thought maybe she would understand it easier, esp. with explanation. But patently I'm way way too old. Dunno about you.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    I'll just chuck another one into the fray.

    Code:
    '#^[\w\.\+\-]{1,}@(?:[\w\-]+\.){1,}[a-z]{2,4}$#i',

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Nope. Doesn't satisfy the requirements.

    Code:
    [\w\.\+\-]{1,}
    says "any letter or digit or undeline or period or plus or dash, repeated 1 or more times."

    *ANY*.

    So that would *pass* an email address of
    Code:
        .....@....xxx
    Lorna wanted to ensure there were never two periods in a row before the @ (she didn't restrict what followed the @).

    (It also doesn't enforce her requirement that the email address start with a letter and end only with ".com", ".net", or ".org" -- but that's a detail. <grin/>)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •