473,398 Members | 2,404 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,398 software developers and data experts.

Regular Expression Problems

At the moment my code is like:

function telValid(inString) {
var regexp = /^[0-9 \+\(\)]+$/;
return (inString.match(regexp,''));
}

which is use to validate phone numbers (UK at the moment)

this works ok, but wont allow - (Dashes) which i need..
tried doing:

function telValid(inString) {
var regexp = /^[0-9 \+\-\(\)]+$/;
return (inString.match(regexp,''));
}
Yet that didn't work either

Any ideas please?
Jun 28 '06 #1
2 1426
Advo wrote:
At the moment my code is like:

function telValid(inString) {
var regexp = /^[0-9 \+\(\)]+$/;
return (inString.match(regexp,''));
}

which is use to validate phone numbers (UK at the moment)

this works ok, but wont allow - (Dashes) which i need..


The dash must come first:

function telValid(inString) {
var regexp = /^[-0-9 \+()]+$/;
return (inString.match(regexp,'')); }

var p=telValid("23)4-4(56");
alert (p ? "yes" : "no"); // => yes

Csaba Gabor from Vienna

Jun 28 '06 #2
JRS: In article <fS******************@newsfe2-gui.ntli.net>, dated Wed,
28 Jun 2006 17:19:07 remote, seen in news:comp.lang.javascript, Advo
<x@x.com> posted :
At the moment my code is like:

function telValid(inString) {
var regexp = /^[0-9 \+\(\)]+$/;
return (inString.match(regexp,''));
}

which is use to validate phone numbers (UK at the moment)

this works ok, but wont allow - (Dashes) which i need..

It will allow ')(' as a number, also ' '.

There's no point in validating at all unless you can do a better job
than just ensuring no invalid characters and at least one character.

First, list the allowable formats. You seem to want to allow +44;
should the "STD" part, (0)20 for London, be optional?

BTW, the numbers of phones within the UK are not necessarily UK phone
numbers (the inverse Ley effect); there are foreigners among us.

You should end up with something more like

function telValid(inString) {
return /^(\(\+\d{1,3}\))?\d{8,11}$/.test(inString.replace(/[- ]/g, "")) }

which accepts (+474) 077 667778

but that's probably too short and the parentheses are probably
mis-placed.

--
© 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.
Jun 29 '06 #3

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

Similar topics

5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
5
by: Bradley Plett | last post by:
I'm hopeless at regular expressions (I just don't use them often enough to gain/maintain knowledge), but I need one now and am looking for help. I need to parse through a document to find a URL,...
2
by: Brian Kitt | last post by:
I have a process where I do some minimal reformating on a TAB delimited document to prepare for DTS load. This process has been running fine, but I recently made a change. I have a Full Text...
1
by: Shawn B. | last post by:
Greetings, I'm using a custom WebBrowser control: http://www.codeproject.com/KB/miscctrl/csEXWB.aspx When I get the DocumentSource of a web page I browsed, and run a regular expression...
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?
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
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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...

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.