jrutter
jrutter

Reputation: 3213

How to modify regex for phone numbers and accept only digits

I have this following regex method for the jquery validate plugin.

jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
    return this.optional(element) || phone_number.length > 9 &&
        phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");

Currently, its validating against phone numbers in this format : 203-123-1234

I need to change to validate like this: 2031231234

Does anyone have a quick and easy solution for me?

Upvotes: 0

Views: 9180

Answers (6)

Dmitry F
Dmitry F

Reputation: 1670

I think better approach would be changing the whole expression with simpler version, something like this:

/^[0-9]{10}$/

Edited, Note (see comments):

This is just a limited example of how to validate a format: 111-222-3333 vs 1112223333, not proper US phone number validation.

Upvotes: 2

Exort
Exort

Reputation: 1075

You can use if you want to match all formats, including 203-123-1234 and 2031231234

EDIT : I'm no regex expert, but I added "1-" support

/^(?:1-?)?[(]?\d{3}[)]?\s?-?\s?\d{3}\s?-?\s?\d{4}$/

By the way, there's a really nice AIR tool for regex, it's called RegExr and you can get the desktop version here http://www.gskinner.com/RegExr/desktop/ or use the online version http://gskinner.com/RegExr/ . There's also a "community" section that contains a lot of useful working regex. That's where I took that one.

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881663

Getting rid of all those -? sequences is probably the quickest way - they mean zero or one - characters.

That will reduce it to:

/^(1)?(\([2-9]\d{2}\)|[2-9]\d{2})[2-9]\d{2}\d{4}$/

whih can be further simplified to:

/^1?(\([2-9]\d{2}\)|[2-9]\d{2})[2-9]\d{6}$/

If you also want to disallow the brackets around area codes, you can further simplify it to:

/^1?[2-9]\d{2}[2-9]\d{6}$/

(and, technically, it won't match the literal 203-123-1234 since the character immediately after that first - has to be 2 thru 9, so I'm assuming you were just talking about the format rather than the values there).

Upvotes: 2

yoozer8
yoozer8

Reputation: 7489

If you just want ten digits, then

phone_number.match(/\d{10}/)

will do it. If you want to match any of the other conditions in there (eg match both 1-2031231234 and 2031231234), you will need to add more.

As a side note, what you currently have doesn't match 203-123-1234 because the first digit after the first hyphen is a 1, and it is looking for 2-9 in that spot.

Upvotes: 1

Jayantha Lal Sirisena
Jayantha Lal Sirisena

Reputation: 21366

([0-9]{10}) this will match with 10 digit number.

Upvotes: 0

Jason Gennaro
Jason Gennaro

Reputation: 34855

You can replace

phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);

with this

phone_number.match(/\d{10}/);

\d means match any digit

and

{10} means 10 times

Upvotes: 3

Related Questions