NANP 电话号码的 jQuery 验证
我正在尝试验证我的表格。我的表单中有一个电话号码字段。用户插入电话号码,然后进行验证和格式化。我在 StackOverflow 上阅读了很多其他类似的问题,并通过其他地方的搜索,但我被困住了。表格一直提示我插入的有效号码无效。
我只关心 NANP 数字,我知道 NANP 数字的格式如下:NXX-NXX-XXXX,其中 N 只能是数字 [2-9]
,X 可以是 [0-9]
。我不需要国家代码。
这是我的代码:
function validatePhone(){
var error = 1;
var hasError = false;
var $this = $(this);
var regex1 = /^(\()?[2-9]{1}\d{2}(\))?(-|\s)?[2-9]{1}\d{2}(-|\s)\d{4}$/;
phone = $(this).val();
phone = phone.replace(/[^0-9]/g,'');
if(!regex1.test(phone)){
hasError = true;
$this.css('background-color','#FFEDEF');
}
else{
area = phone.substring(0,3);
prefix = phone.substring(3,6);
line = phone.substring(6);
$this.val('(' + area + ') ' + prefix + '-' + line);
$this.css('background-color','#FFFFFF');
}
这个想法是,无论我是否插入 8012559553、(801)2559553、(801)255-9553、801-255-9553 或类似的内容,它都会被验证和格式化,如下所示:(801) 255- 9553。但同样,由于某种原因,表单继续说我插入的任何数字都是无效的,无论它是否有效。这是我正在使用的代码,该代码可以工作,但不符合 NANP 格式:
function validatePhone(){
var error = 1;
var hasError = false;
var $this = $(this);
phone = $(this).val();
phone = phone.replace(/[^0-9]/g,'');
if(phone.length != 10){
hasError = true;
$this.css('background-color','#FFEDEF');
}
else{
area = phone.substring(0,3);
prefix = phone.substring(3,6);
line = phone.substring(6);
$this.val('(' + area + ') ' + prefix + '-' + line);
$this.css('background-color','#FFFFFF');
}
因此,我在实现数字的正则表达式测试以确保数字是真实的并且数字格式正确时遇到问题。 ..有什么想法吗?
I'm trying to validate my form. In my form is a phone number field. The user inserts a phone number and validation takes place as well as formatting. I've read a lot of the other similar questions in StackOverflow and through searches elsewhere, but I'm stuck. The form keeps saying that a valid number I insert is invalid.
I'm only concerned with NANP numbers and I understand the NANP numbers are formatted like this: NXX-NXX-XXXX, where N can only be digits [2-9]
, and X can be [0-9]
. I don't need a country code.
Here's my code:
function validatePhone(){
var error = 1;
var hasError = false;
var $this = $(this);
var regex1 = /^(\()?[2-9]{1}\d{2}(\))?(-|\s)?[2-9]{1}\d{2}(-|\s)\d{4}$/;
phone = $(this).val();
phone = phone.replace(/[^0-9]/g,'');
if(!regex1.test(phone)){
hasError = true;
$this.css('background-color','#FFEDEF');
}
else{
area = phone.substring(0,3);
prefix = phone.substring(3,6);
line = phone.substring(6);
$this.val('(' + area + ') ' + prefix + '-' + line);
$this.css('background-color','#FFFFFF');
}
The idea is that no matter whether I insert 8012559553, (801)2559553, (801)255-9553, 801-255-9553 or something similar that it would be validated and formatted like this: (801) 255-9553. But again, the form for some reason continues to say that any number I insert is invalid, regardless of whether it's valid or not. This is code that I was using that was working, but didn't comply with NANP formats:
function validatePhone(){
var error = 1;
var hasError = false;
var $this = $(this);
phone = $(this).val();
phone = phone.replace(/[^0-9]/g,'');
if(phone.length != 10){
hasError = true;
$this.css('background-color','#FFEDEF');
}
else{
area = phone.substring(0,3);
prefix = phone.substring(3,6);
line = phone.substring(6);
$this.val('(' + area + ') ' + prefix + '-' + line);
$this.css('background-color','#FFFFFF');
}
So, I'm having trouble implementing the regex test of the numbers to make sure that the numbers are real and then have the number formatted correctly... Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的不是很严格但很好的正则表达式:
它接受:
它不接受:
Here is my not very strict but yet nice RegExp:
It accepts:
It doesn't accepts:
您的代码的问题在于
regex1
正在查找phone
字符串中的特殊字符。该代码失败,因为您要从phone = phone.replace(/[^0-9]/g,'');
phone 字符串中删除所有特殊字符>,因此当您针对regex1
进行测试时,您正在寻找已删除的值。无论如何,你的方法没问题。我经常发现使用两个简单的正则表达式比尝试将所有内容都放入一个包罗万象的正则表达式中更容易。一旦您将 regex1 修改为仅对不带特殊字符的 10 位数字进行 NANP 有效性检查,您的代码将正常工作。
The problem with your code is that
regex1
is looking for special characters in thephone
string. The code fails because you are stripping all special characters from thephone
string in the linephone = phone.replace(/[^0-9]/g,'');
, and thus when you do your test againstregex1
you are looking for values that you already removed.By all means, your approach is fine. Often I find it easier to use two simple REGEXs instead of trying to fit everything into one catch-all REGEX. Once you modify regex1 to only do a NANP validity check on a 10 digit number with no special characters, your code will work fine.