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:
A period can occur before the @, but not twice in a row -- as in jane.doe@xyz.com but not jane..doe@xyz.com.
Email must start with a letter.
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.
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!
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
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.
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.
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.