NANP 电话号码的 jQuery 验证

发布于 2024-11-27 07:46:47 字数 1701 浏览 4 评论 0原文

我正在尝试验证我的表格。我的表单中有一个电话号码字段。用户插入电话号码,然后进行验证和格式化。我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捂风挽笑 2024-12-04 07:46:47

这是我的不是很严格但很好的正则表达式:

/^\D*([2-9]\d{2}\D*){2}(\d{2}\D*){2}\D*$/

它接受:

  • (235)-546-42-09
  • 452-845-12-12
  • [933]-323-34-53
  • 222-435-0903
  • 222-4350903
  • 2224350903
  • ( 222 ) -- 785 -- 12 -- 76

它不接受:

  • 111-342-45-45
  • 234-111-50-78
  • 222-222-222-2(您可以简单地修改 RegExp 来删除它)

Here is my not very strict but yet nice RegExp:

/^\D*([2-9]\d{2}\D*){2}(\d{2}\D*){2}\D*$/

It accepts:

  • (235)-546-42-09
  • 452-845-12-12
  • [933]-323-34-53
  • 222-435-0903
  • 222-4350903
  • 2224350903
  • ( 222 ) -- 785 -- 12 -- 76

It doesn't accepts:

  • 111-342-45-45
  • 234-111-50-78
  • 222-222-222-2 (you may simply modify RegExp to remove this)
静水深流 2024-12-04 07:46:47

您的代码的问题在于 regex1 正在查找 phone 字符串中的特殊字符。该代码失败,因为您要从 phone = phone.replace(/[^0-9]/g,'');phone 字符串中删除所有特殊字符>,因此当您针对 regex1 进行测试时,您正在寻找已删除的值。

无论如何,你的方法没问题。我经常发现使用两个简单的正则表达式比尝试将所有内容都放入一个包罗万象的正则表达式中更容易。一旦您将 regex1 修改为仅对不带特殊字符的 10 位数字进行 NANP 有效性检查,您的代码将正常工作。

function validatePhone(){
    var error = 1;

    var hasError = false;
    var $this = $(this);
    var regex1 = /^([2-9]{1}\d{2})([2-9]{1}\d{2})\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 problem with your code is that regex1 is looking for special characters in the phone string. The code fails because you are stripping all special characters from the phone string in the line phone = phone.replace(/[^0-9]/g,'');, and thus when you do your test against regex1 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.

function validatePhone(){
    var error = 1;

    var hasError = false;
    var $this = $(this);
    var regex1 = /^([2-9]{1}\d{2})([2-9]{1}\d{2})\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');    
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文