JavaScript Regex validation - simple list of allowed chars

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

LVL 12
Barry JonesAsked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
if you want to allow whitespace :

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

Open in new window

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

Open in new window

0
 
Barry JonesAuthor Commented:
Awesome!  Thanks for your quick response..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.