JRS: In article <f7**************************@posting.google.com >,
dated Tue, 28 Sep 2004 15:45:51, seen in news:comp.lang.javascript, Ward
Cleaver <fr*******@yahoo.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.7890" 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.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.