473,383 Members | 1,788 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,383 software developers and data experts.

very simple reg evaluation

Hello Gurus,
I wanted to have a very simple, strict regular expression validator for a
phone field. the only valid format is
(XXX) XXX-XXXX where X is a digit
I have the following but it always evaluates to false

var reg = new RegExp("^\(\d{3}\) \d{3}-\d{4}$");
alert(reg.test("(123) 221-9244"));

Any help is greatly appreciated..
Jul 23 '05 #1
5 1745
Henok Girma wrote on 06 nov 2004 in comp.lang.javascript:
Hello Gurus,
I wanted to have a very simple, strict regular expression validator
for a phone field. the only valid format is
(XXX) XXX-XXXX where X is a digit
I have the following but it always evaluates to false

var reg = new RegExp("^\(\d{3}\) \d{3}-\d{4}$");
alert(reg.test("(123) 221-9244"));

Any help is greatly appreciated..


var reg = /^\(\d{3}\) \d{3}-\d{4}$/;
alert(reg.test("(123) 221-9244"));

However, even(!) in the States there seem to be phone numbers, that cannot
be described in that format.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #2
Henok Girma wrote:
Hello Gurus,
I wanted to have a very simple, strict regular expression validator
for a phone field. the only valid format is
(XXX) XXX-XXXX where X is a digit
I have the following but it always evaluates to false

var reg = new RegExp("^\(\d{3}\) \d{3}-\d{4}$");
alert(reg.test("(123) 221-9244"));

Any help is greatly appreciated..


Hi,

You misplaced the 4th backslash

-\ should be \-

Fred
Jul 23 '05 #3
Fred Serry wrote on 06 nov 2004 in comp.lang.javascript:
Henok Girma wrote:
Hello Gurus,
I wanted to have a very simple, strict regular expression validator
for a phone field. the only valid format is
(XXX) XXX-XXXX where X is a digit
I have the following but it always evaluates to false

var reg = new RegExp("^\(\d{3}\) \d{3}-\d{4}$");
alert(reg.test("(123) 221-9244"));

Any help is greatly appreciated..


Hi,

You misplaced the 4th backslash

-\ should be \-


Why?

1 that is the 5th you are talking about
2 - does not have to be escaped
3 d without the \ is just d, not [0-9]
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #4
thank you, it's working fine!
i can't think of a number that does not follow the (XXX) XXX-XXXX because 1
before it is arbitrary

i have a function that will format it that way the minute the the text box
loses focus.. so if the user enters
498 598 9864
229-569-6974
54 654a65s4 a6s54a6s52324
or any combination it will be filtered and tries to create a (XXX) XXX-XXXX
format phone, it works just fine, but i want to do a final check after the
script formats it, because if they don't finish giving all the digits, the
phone number can be wrong.. otherwise it's always correct...

in anycase, this will do just fine for the intranet application i am working
on, once again thank you for all the help!!
"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.29...
Henok Girma wrote on 06 nov 2004 in comp.lang.javascript:
Hello Gurus,
I wanted to have a very simple, strict regular expression validator
for a phone field. the only valid format is
(XXX) XXX-XXXX where X is a digit
I have the following but it always evaluates to false

var reg = new RegExp("^\(\d{3}\) \d{3}-\d{4}$");
alert(reg.test("(123) 221-9244"));

Any help is greatly appreciated..


var reg = /^\(\d{3}\) \d{3}-\d{4}$/;
alert(reg.test("(123) 221-9244"));

However, even(!) in the States there seem to be phone numbers, that cannot
be described in that format.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #5
Evertjan. wrote:
Fred Serry wrote on 06 nov 2004 in comp.lang.javascript:
Henok Girma wrote:
Hello Gurus,
I wanted to have a very simple, strict regular expression validator
for a phone field. the only valid format is
(XXX) XXX-XXXX where X is a digit
I have the following but it always evaluates to false

var reg = new RegExp("^\(\d{3}\) \d{3}-\d{4}$");
alert(reg.test("(123) 221-9244"));

Any help is greatly appreciated..


Hi,

You misplaced the 4th backslash

-\ should be \-


Why?

1 that is the 5th you are talking about
2 - does not have to be escaped
3 d without the \ is just d, not [0-9]


You're right. my flaw.

Fred
Jul 23 '05 #6

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

Similar topics

11
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
3
by: Thomas Ibbotson | last post by:
I've been going through a tutorial on C++ and I've come across what I regard as some strange behaviour. As part of the tutorial on classes a simple stack is implemented with functions push() and...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
77
by: berns | last post by:
Hi All, A coworker and I have been debating the 'correct' expectation of evaluation for the phrase a = b = c. Two different versions of GCC ended up compiling this as b = c; a = b and the other...
17
by: hanumizzle | last post by:
I have used Perl for a long time, but I am something of an experimental person and mean to try something new. Most of my 'work' with Vector Linux entails the use of Perl (a bit of a misnomer as it...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
6
by: Bob | last post by:
Hi, I have a fairly large but not massive project. Visual Studio 2005 is starting to act very strange when I debug the project. For instance, if i set a break point, I often can't expand an...
1
by: =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?= | last post by:
Hi, A while ago I asked a question on the list about a simple eval function, capable of eval'ing simple python constructs (tuples, dicts, lists, strings, numbers etc) in a secure manner:...
39
by: Boltar | last post by:
Why does C do lazy evaluation for logical boolean operations but not bitwise ones? Ie: the following program prints "1 2" , not "1 1" under gcc main() { int a = 1; int b = 1; 0 && ++a;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.