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

javascript - Regex for 8 digit phone number (Singapore number length) - Stack Overflow

programmeradmin1浏览0评论

I'm trying to validate my phone address text field. Can anyone provide me a simple regex to either validate for numeric digits only or a specific 8 digit regex?

Much thanks!

$('#phone').on('input', function() {
var input = $( this );
var regex = /* regex needed here */
var is_phone = regex.test(input.val());
if (is_phone){input.removeClass("invalid").addClass("valid");}
else {input.removeClass("valid").addClass("invalid");}
});

I'm trying to validate my phone address text field. Can anyone provide me a simple regex to either validate for numeric digits only or a specific 8 digit regex?

Much thanks!

$('#phone').on('input', function() {
var input = $( this );
var regex = /* regex needed here */
var is_phone = regex.test(input.val());
if (is_phone){input.removeClass("invalid").addClass("valid");}
else {input.removeClass("valid").addClass("invalid");}
});
Share Improve this question edited May 9, 2016 at 11:07 georg 215k56 gold badges322 silver badges400 bronze badges asked May 9, 2016 at 11:04 iamhxiamhx 4727 silver badges25 bronze badges 1
  • There are literally thousands of examples out there. The pattern is simple: ^\d{8}$ – Terry Commented May 9, 2016 at 11:08
Add a ment  | 

1 Answer 1

Reset to default 3

It will allow 0-9 numbers and only 8 digits. Try this:

var regex = '/^[0-9]{1,8}$/'; // if you want min 1 and max 8 numbers

var regex = '/^[0-9]{8}$/'; // if you want exact 8 numbers
发布评论

评论列表(0)

  1. 暂无评论