473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

RegExp to validate an email address

I was reading my O'Reilly JavaScript The Definitive Guide when I came
across RegExp and thought I could tighten up my JavaScript code that
checks for a valid email address. Why does the following not appear to work:

var email_address = "Joe@Schmoe";
var email_regex = new RegExp ("^(\\w+)(\@)(\\w+)(\.)(\\w+)$");
var result = email_regex.exec (email_address);
alert (" result[1] = \"" + result[1] + "\"\n" +
" result[2] = \"" + result[2] + "\"\n" +
" result[3] = \"" + result[3] + "\"\n" +
" result[4] = \"" + result[4] + "\"\n" +
" result[5] = \"" + result[5] + "\"");

The resulting alert box contains:

result[1] = "Joe"
result[2] = "@"
result[3] = "Schm"
result[4] = "o"
result[5] = "e"

It appears that the "\." is not escaping the "." to mean the literal
period. What am I doing wrong?
--
How come you don't ever hear about gruntled employees? And who has been
dissing them anyhow?

Jul 20 '05 #1
10 7641
> It appears that the "\." is not escaping the "." to mean the
literal
period. What am I doing wrong?


You have "\." when you meant to put "\\." in the RegExp string.

Jul 20 '05 #2
Andrew DeFaria wrote:
[...]
var email_regex = new RegExp ("^(\\w+)(\@)(\\w+)(\.)(\\w+)$"
[...]
It appears that the "\." is not escaping the "." to mean the literal
period. What am I doing wrong?
You did not notice consistently that the backslash is in string literals an
escape character, too. Therefore you need to escape it with another `\' if
you want a literal `\' to be passed to the RegExp constructor function, as
you did before and after. Otherwise it is ignored unless the combination
with the following character (here: `.') forms a known escape sequence, e.g.
`\n':

var email_regex = new RegExp ("^(\\w+)(@)(\\w+)(\\.)(\\w+)$"

But you can use RegExp literals instead which makes the Regular Expression
more legible since you need not to escape escape characters of strings:

var email_regex = /^(\w+)(@)(\w+)(\.)(\w+)$/;

BTW: In JavaScript, `@' need not to be escaped neither in string nor in
RegExp literals.
PointedEars

P.S.:
| Content-Type: multipart/alternative;
| boundary="------------020801000303080605020808"

Please don't post Multipart HTML, Usenet is (except of
newsgroups where binaries are accepted) a text-only medium.
Uncheck Edit, Mail & Newsgroups Account Settings, $News_account,
Composition and Addressing, Compose messages in HTML format.
--


This preference setting will also make your signature separator
correct and working.
Jul 20 '05 #3
Thomas 'PointedEars' Lahn wrote:
Andrew DeFaria wrote:
[...]
var email_regex = new RegExp ("^(\\w+)(\@)(\\w+)(\.)(\\w+)$"
[...]
It appears that the "\." is not escaping the "." to mean the literal
period. What am I doing wrong?
You did not notice consistently that the backslash is in string
literals a escape character, too. Therefore you need to escape it with
another `\' if you want a literal `\' to be passed to the RegExp
constructor function, as you did before and after. Otherwise it is
ignored unless the combination
with the following character (here: `.') forms a known escape
sequence, e.g. `\n':

var email_regex = new RegExp ("^(\\w+)(@)(\\w+)(\\.)(\\w+)$"


I had tried that. When I did the the following fails:

var email_address = "Joe@Schmoe";
var email_regex = new RegExp ("^(\\w+)@(\\w+)(\\.)(\\w+)$");
var result = email_regex.exec (email_address);
alert ("Checking results");
alert (" result[0] = \"" + result[0] + "\"");
alert (" result[1] = \"" + result[1] + "\"");
alert (" result[2] = \"" + result[2] + "\"");
alert (" result[3] = \"" + result[3] + "\"");
alert (" result[4] = \"" + result[4] + "\"");

I get the "Checking results" alert but I do not get the result[0] alert.
As such I figured that the \\ in the \\. caused the problem.
But you can use RegExp literals instead which makes the Regular
Expression more legible since you need not to escape escape characters
of strings:

var email_regex = /^(\w+)(@)(\w+)(\.)(\w+)$/;
You know I wanted to use that too but evertime I did my JavaScript just
blew up. Now it works! I think I forget the trailing ";" (I've been
writing a lot of Tcl at work where semicolons are not used at the end of
line).
BTW: In JavaScript, `@' need not to be escaped neither in string nor
in RegExp literals.


I thought that but I was just backslashing it in case...

OK I think it got it working now. Thanks.
--
Lottery: A tax on people who are bad at math.

Jul 20 '05 #4
JRS: In article <22***************************@msgid.meganewsserve rs.co
m>, seen in news:comp.lang.javascript, Andrew DeFaria
<An****@DeFaria.com> posted at Tue, 28 Oct 2003 23:24:12 :-

var email_address = "Joe@Schmoe";
var email_regex = new RegExp ("^(\\w+)(\@)(\\w+)(\.)(\\w+)$");
var result = email_regex.exec (email_address);
alert (" result[1] = \"" + result[1] + "\"\n" +
" result[2] = \"" + result[2] + "\"\n" +
" result[3] = \"" + result[3] + "\"\n" +
" result[4] = \"" + result[4] + "\"\n" +
" result[5] = \"" + result[5] + "\"");

The resulting alert box contains:

result[1] = "Joe"
result[2] = "@"
result[3] = "Schm"
result[4] = "o"
result[5] = "e"

It appears that the "\." is not escaping the "." to mean the literal
period. What am I doing wrong?


(1) posting in HTML/multi-part.

(2) having \ but not \\ before the dot.

ISTM better to express RegExps as literals -
RE = /^(\w+)(\@)(\w+)(\.)(\w+)$/

I see no need to escape the @,
nor to parenthesise the fixed parts @ and \. .

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #5
Would you please configure your news client correctly as
I asked and described at the bottom of my previous posting?

Andrew DeFaria wrote:
Thomas 'PointedEars' Lahn wrote:
var email_regex = new RegExp ("^(\\w+)(@)(\\w+)(\\.)(\\w+)$"
I had tried that. When I did the the following fails:

var email_address = "Joe@Schmoe";
var email_regex = new RegExp ("^(\\w+)@(\\w+)(\\.)(\\w+)$");
var result = email_regex.exec (email_address);
alert ("Checking results");

52: alert (" result[0] = \"" + result[0] + "\"");
alert (" result[1] = \"" + result[1] + "\"");
alert (" result[2] = \"" + result[2] + "\"");
alert (" result[3] = \"" + result[3] + "\"");
alert (" result[4] = \"" + result[4] + "\"");

I get the "Checking results" alert but I do not get the result[0] alert.
As such I figured that the \\ in the \\. caused the problem.


Yes, but different than you think. The above is the correct RegExp but it
only matches strings that contain a dot. The value of `email_address' here
("Joe@Schmoe") simply does not contain a dot, so there is no match and
`result' therefore has no zero-property or no zeroth subexpression match
(which would be equal to the string the whole RegExp matches):

| Error: result has no properties
| Source File: http://localhost/scripts/test/test2
| Line: 52

In contrast, "\." in the string which resulted in the subexpression /./,
matches *any* character, namely the last `o' (since the second \w+ is
greedy and the last \w+ matches `e'), so result[0] is "Joe@Schmoe" and
not `undefined'.

Try

alert(new RegExp("^(\\w+)@(\\w+)(\\.)(\\w+)$").exec("Joe@Sch moe"));

and

alert(new RegExp("^(\\w+)@(\\w+)(\.)(\\w+)$").exec("Joe@Schm oe"));

to find this confirmed.
var email_regex = /^(\w+)(@)(\w+)(\.)(\w+)$/;


You know I wanted to use that too but evertime I did my JavaScript just
blew up. Now it works! I think I forget the trailing ";" (I've been
writing a lot of Tcl at work where semicolons are not used at the end of
line).


The trailing `;' is optional (though recommended) unless you have
bad[tm] line-optimized code with two or more statements in a line.
HTH

PointedEars
Jul 20 '05 #6
Thomas 'PointedEars' Lahn wrote:
Would you please configure your news client correctly as I asked and
described at the bottom of my previous posting?


Ah.... No.

--
I used to work at a factory where they made hydrants; but you couldn't
park anywhere near the place.

Jul 20 '05 #7

"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:3F**************@PointedEars.de...
Would you please configure your news client correctly as I asked and

described at the bottom of my previous posting?

Would you please reconfigure yours as I asked *you*? Your line lengths
are terrible!
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #8
Fabian wrote:
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:3F**************@PointedEars.de...
Would you please configure your news client correctly as I asked and described at the bottom of my previous posting?

Would you please reconfigure yours as I asked *you*?


Message-ID, please!
Your line lengths are terrible!


I don't know what's wrong with your news client (in fact I do know:
it's b0rken by default,) but mine is doing line-break automagically at
76 characters (except of URIs) as supposed, according to the standards:

,--------------<http://www.rfc-editor.org/rfc/rfc2822.txt>---------------
| 2.3. Body
|
| The body of a message is simply lines of US-ASCII characters. The
| only two limitations on the body are as follows:
|
| [...]
|
| - Lines of characters in the body MUST be limited to 998 characters,
| and SHOULD be limited to 78 characters, excluding the CRLF.

If you don't believe me, let Google Groups be the referee:

http://groups.google.com/groups?q=ms...UTF-8&oe=UTF-8
HTH & HAND

PointedEars
Jul 20 '05 #9
"Fabian" <la****@hotmail.com> writes:
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:3F**************@PointedEars.de...
Would you please configure your news client correctly as I asked and

described at the bottom of my previous posting?

Would you please reconfigure yours as I asked *you*? Your line lengths
are terrible!


When I read the above two lines in the posting you reply to, none of
them is more than 72 characters. My newsreader doesn't wrap quotes
auotmatically, so I believe the problem is with your software.

In the message you replied to, there was only two lines (of
non-quotation) that were longer than 72 characters, and they were 73
and 74 respectively. Not something I would complain about.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Demo: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #10
JRS: In article <wu**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen
<lr*@hotpop.com> posted at Thu, 30 Oct 2003 11:37:54 :-

In the message you replied to, there was only two lines (of
non-quotation) that were longer than 72 characters, and they were 73
and 74 respectively. Not something I would complain about.


Agreed. Various RFCs and other sources suggest various margin
settings; for example FYI28 mentions 65 characters.

Given that there is a general expectation that most quoted lines
will not exceed 80 characters, and that some quoters use " >" as
indicator, ISTM that the right margin setting should not usually
exceed 72 characters for optimum results.

If the composing software permits quick altering of the margin
setting of a completed article, it may be worth trying a small
change when the text looks particularly ugly. I narrowed this
article a little, and the paragraphs are now almost justified.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Karuna | last post by:
Hi If I want to validate certain IP Address range say in an IP Address range of : "15.70.186.15-100", here where 15.70.186 is the network id and 15-100 is the host id. I want to discover all IP...
12
by: Dag Sunde | last post by:
My understanding of regular expressions is rudimentary, at best. I have this RegExp to to a very simple validation of an email-address, but it turns out that it refuses to accept mail-addresses...
6
by: micklee74 | last post by:
hi i created a script to ask user for an input that can be a pattern right now, i use re to compile that pattern pat = re.compile(r"%s" %(userinput) ) #userinput is passed from command line...
3
by: c676228 | last post by:
Hi everyone, I just realized that it's so important to validate each string, I mean 'each' before you insert data from asp page into database. I guess some customers just copy data from some...
23
by: codefire | last post by:
Hi, I am trying to get a regexp to validate email addresses but can't get it quite right. The problem is I can't quite find the regexp to deal with ignoring the case james..kirk@fred.com, which...
11
by: TokyoJ | last post by:
I run a small camp in Alaska for kids and my director is asking for a web form. Could someone please have a look and offer some advice on where I'm making mistake(s)? I'm using the RegExp function...
1
by: SkipNRun | last post by:
I am a novice when comes to JavaScript, AJAX. I am working on a form, which will allow users to update their contact information. In order to make the form flexible, I need to use pull down list. ...
8
by: Ben Amada | last post by:
Hi all. I know very little about regular expressions, but wanted to use one to validate an email address a user would be entering before the form is submitted. There are many examples out there. ...
1
by: saravanatmm | last post by:
I need javascript code for validate the email address. Email address field cannot allowed the capital letters, special characters except '@' symbol. But can allowed the small letters, numeric...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.