Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JavaScript Regex validation - simple list of allowed chars

Posted on 2011-09-26
3
Medium Priority
?
158 Views
Last Modified: 2012-05-12
Hi.  

Please can someone show me the correct regex for ensuring input has ONLY the following list of chars, in any order:

0123456789-+()

Any number of whitespace is allowed.

My test code attached.

TIA, TheFoot


function(v_val){
	return /^[0-9-+]$/.test(v_val);
}

Open in new window

0
Comment
Question by:Barry Jones
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36599741
function(v_val){
	return !/[^\d-+()]/.test(v_val);
}

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 36599749
if you want to allow whitespace :

function(v_val){
	return !/[^\d\s-+()]/.test(v_val);
}

Open in new window

0
 
LVL 12

Author Closing Comment

by:Barry Jones
ID: 36600224
Awesome!  Thanks for your quick response..
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question