最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to modify regex for phone numbers and accept only digits - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited May 28, 2014 at 16:28 Kara 6,22616 gold badges53 silver badges58 bronze badges asked Sep 1, 2011 at 15:08 jrutterjrutter 3,21310 gold badges45 silver badges51 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 3

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

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).

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

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

Edited, Note (see ments):

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

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.

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

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./RegExr/desktop/ or use the online version http://gskinner./RegExr/ . There's also a "munity" section that contains a lot of useful working regex. That's where I took that one.

发布评论

评论列表(0)

  1. 暂无评论