Anonymous
Anonymous

Reputation: 3689

Negated character not working as expected ? ^

I want to allow my users to enter a phone number and choose their own way of seperating the numbers (or not). So I came up with a regex:

var regex = /[^0-9 \/-\\\(\)\+]/;

In most cases it works fine, but there are some examples like @,:,; where it doesn't work as I would expect. Could someone give me some hint please ?

Here's an example of what I mean

testvar = '123@213';
var regex = /[^0-9 \/-\\\(\)\+]/;
if(regex.test(testvar) === true) 
    { alert('Chars out of regex-range found'); } // won't fire!

Upvotes: 1

Views: 88

Answers (2)

kennebec
kennebec

Reputation: 104770

A phone number only uses digits, I'd ignore the user format and just keep the digits

var phonenum=value.replace(/\D+/g,'');

Upvotes: 1

Lyth
Lyth

Reputation: 2211

There's a long way from / to \ if you meant it. And if not, you're missing a slash before the dash:

var regex = /[^0-9 \/\-\\\(\)\+]/;

Upvotes: 4

Related Questions