Solved

JavaScript Regex validation - simple list of allowed chars

Posted on 2011-09-26
3
151 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
  • 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 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

774 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