473,663 Members | 2,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regular expressions and search() - vertical bar problem

Hi. I have an assignment to do some validating of a form using
javascript and mostly the search() method. I'm having problems
getting a positive validation for phone numbers like "123-456-7890"
and "123.456.78 90" but not like "123.456-7890". The regular
expression I'm using now is something like this:
ok = pn.search(/(^\d{3}-\d{3}-\d{4}$)|(^\d{3} .\d{3}.\d{4}$)/);
Which to me looks like it SHOULD do what I want it to and not come
back with an ok=0 for a phone number using both hyphens and periods.
Please help.
Jul 23 '05 #1
6 1954
Ward Cleaver wrote:
ok = pn.search(/(^\d{3}-\d{3}-\d{4}$)|(^\d{3} .\d{3}.\d{4}$)/);


I'm guessing...

(/(^\d{3}(-|.)\d{3}(-|.)\d{4}$)/)

should do the trick for all cases.

You could also use:

(/((\d{3}(-|.)){2}\d{4}$)/)

but that will produce +ve values if extra characters are
added to the front of the test string - only zero (0) will
be a correct result or any non-zero value a fail. For the
sake of saving a few characters, I don't think it's worth
it.

Cheers.
Jul 23 '05 #2
On 28 Sep 2004 15:45:51 -0700, Ward Cleaver <fr*******@yaho o.com> wrote:
Hi. I have an assignment
If you mean an educational course assignment, you're probably required to
mention that you received help.
to do some validating of a form using javascript and mostly the search()
method.
String.prototyp e.search is generally unsuitable for validation. If you
want to assert that a string matches a pattern, use RegExp.prototyp e.test:

/<pattern>/.test(<string>)

which returns true for a match. The search method returns the position
where a match was found.
I'm having problems getting a positive validation for phone numbers like
"123-456-7890" and "123.456.78 90" but not like "123.456-7890". The
regular expression I'm using now is something like this:
ok = pn.search(/(^\d{3}-\d{3}-\d{4}$)|(^\d{3} .\d{3}.\d{4}$)/);
An unescaped dot (period) matches *any* character (except line
terminators). A literal dot needs to written with a backslash prefix.
Which to me looks like it SHOULD do what I want it to and not come back
with an ok=0 for a phone number using both hyphens and periods.
Please help.


Though I recommended the test method, I'd probably use
RegExp.prototyp e.exec so I could check that the separators match:

var r = /^\d{3}([.-])\d{3}([.-])\d{4}$/.exec(pn);

/* If the string didn't match, r will be null.
* If it did match, r will be an array with element
* 0 containing the match
* 1 containing the first remembered match (marked with
* parentheses)
* 2 containing the second remembered match
*/
if(r && (r[1] == r[2])) {
// Pattern matched and the separators are equal.
}

Good luck,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
rh
"Michael Winter" wrote:
<...>

Though I recommended the test method, I'd probably use
RegExp.prototyp e.exec so I could check that the separators match:

var r = /^\d{3}([.-])\d{3}([.-])\d{4}$/.exec(pn);

/* If the string didn't match, r will be null.
* If it did match, r will be an array with element
* 0 containing the match
* 1 containing the first remembered match (marked with
* parentheses)
* 2 containing the second remembered match
*/
if(r && (r[1] == r[2])) {
// Pattern matched and the separators are equal.
}


Alternatively, including a backreference in the regular expression
would allow use of the preferred test method, e.g.:

if ( /^\d{3}([-.])\d{3}\1\d{4}$/.test(pn) ) {
// Pattern matched and the separators are equal.
}

../rh
Jul 23 '05 #4
On 29 Sep 2004 09:20:10 -0700, rh <co********@yah oo.ca> wrote:

[snip]
Alternatively, including a backreference in the regular expression would
allow use of the preferred test method, e.g.:


I didn't know they existed as an ECMA-262 conformant pattern but I have
just found it (section 15.10.2.11). How well supported are they?

Thanks,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5
JRS: In article <f7************ **************@ posting.google. com>,
dated Tue, 28 Sep 2004 15:45:51, seen in news:comp.lang. javascript, Ward
Cleaver <fr*******@yaho o.com> posted :
I have an assignment to do some validating of a form using
javascript and mostly the search() method. I'm having problems
getting a positive validation for phone numbers like "123-456-7890"
and "123.456.78 90" but not like "123.456-7890". The regular
expression I'm using now is something like this:
ok = pn.search(/(^\d{3}-\d{3}-\d{4}$)|(^\d{3} .\d{3}.\d{4}$)/);
Which to me looks like it SHOULD do what I want it to and not come
back with an ok=0 for a phone number using both hyphens and periods.

Dot matches any character; use \. . Testing only with more-or-less
valid data is a distressingly common mistake; a test with a non-allowed
first separator would have given a clue. Build up such expressions in
small stages, testing as you go.

Search returns a number; to get a Boolean for OK, use test.

OK = /^(\d\d\d)([-\.])(\d\d\d)\2(\d\ d\d\d)$/.test(pn)

Be aware that by using that format only you exclude many of the
telephones in North America and elsewhere.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #6
rh
"Michael Winter" <M.******@bluey onder.co.invali d> wrote in message news:<opse3j52g cx13kvk@atlanti s>...
On 29 Sep 2004 09:20:10 -0700, rh <co********@yah oo.ca> wrote:

[snip]
Alternatively, including a backreference in the regular expression would
allow use of the preferred test method, e.g.:


I didn't know they existed as an ECMA-262 conformant pattern but I have
just found it (section 15.10.2.11). How well supported are they?


RegExp backreferences were present in v1.2. I believe it's possible to
use said same with gay abandon :).

../rh
Jul 23 '05 #7

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

Similar topics

8
2423
by: Michael McGarry | last post by:
Hi, I am horrible with Regular Expressions, can anyone recommend a book on it? Also I am trying to parse the following string to extract the number after load average. ".... load average: 0.04, 0.02, 0.01" how can I extract this number with RE or otherwise?
1
4167
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
9
3147
by: Harry | last post by:
Hi there, does anyone know how I can build a regular expression e.g. for the string.search() function on runtime, depending on the content of variables? Should be something like this: var strkey = "something"; var str = "Somethin like this"; if( str.search( / + strkey + / ) > -1 )
7
371
by: norton | last post by:
Hello, Does any one know how to extact the following text into 4 different groups(namely Date, Artist, Album and Quality)? - Artist - Album Artist - Album - Artist - Album - Artist - Album- i have try this syntax but it failed
4
4826
by: lucky | last post by:
hi there!! i'm looking for a code snipett wich help me to search some words into a particular string and replace with a perticular word. i got a huge data string in which searching traditional way mean to secrife lots of time in asp.net. can any one give me such a expression in which i pass a data string and search word string and replace word string? if so plz help me out. i'm in badly need.
0
1948
by: peridian | last post by:
Hi, I wanted a web page where I could post code to, and have it appear in coloured formatting based on the context of the code. Most of the techniques I have seen for this involve complex use of string manipulation where they seek through the string back and forth doing replacements to substitute in the needed HTML code. I am convinced that this can be done with a few regular expressions. Unfortunately my knowledge of regular...
7
2342
by: oscartheduck | last post by:
Hi folks, I'm trying to alter a program I posted about a few days ago. It creates thumbnail images from master images. Nice and simple. To make sure I can match all variations in spelling of jpeg, and different cases, I'm using regular expressions. The code is currently:
12
2462
by: FAQEditor | last post by:
Anybody have any URL's to tutorials and/or references for Regular Expressions? The four I have so far are: http://docs.sun.com/source/816-6408-10/regexp.htm http://en.wikipedia.org/wiki/Regular_expression http://www.regular-expressions.info/javascript.html http://www.webreference.com/js/column5/
47
3419
by: Henning_Thornblad | last post by:
What can be the cause of the large difference between re.search and grep? This script takes about 5 min to run on my computer: #!/usr/bin/env python import re row="" for a in range(156000): row+="a"
0
8345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8858
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8548
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8634
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5657
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.