Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-21-2010, 09:04 PM   PM User | #1
Lorna
New to the CF scene

 
Join Date: Feb 2010
Location: Washington State
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Lorna is an unknown quantity at this point
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!
Lorna is offline   Reply With Quote
Old 02-22-2010, 05:52 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is online now   Reply With Quote
Old 02-23-2010, 01:28 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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 ???
__________________
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.

Last edited by Old Pedant; 02-23-2010 at 01:31 AM..
Old Pedant is offline   Reply With Quote
Old 02-23-2010, 08:02 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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..
Philip M is online now   Reply With Quote
Old 02-23-2010, 08:40 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 02-23-2010, 08:45 PM   PM User | #6
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
I'll just chuck another one into the fray.

Code:
'#^[\w\.\+\-]{1,}@(?:[\w\-]+\.){1,}[a-z]{2,4}$#i',
MattF is offline   Reply With Quote
Old 02-24-2010, 01:25 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
email, javascript, regexp, validation

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:27 AM.


Advertisement
Log in to turn off these ads.