Javascript Regex - 用什么来验证电话号码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14639973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:27:57  来源:igfitidea点击:

Javascript Regex - What to use to validate a phone number?

javascriptregexvalidationwhitespacephone-number

提问by user2031340

Could anyone tell me what RegEx would work to validate an international phone number including white space between the numbers and also allowing for these chars: - ( ).

谁能告诉我什么 RegEx 可以用来验证国际电话号码,包括号码之间的空格并允许这些字符:- ( ).

The amount of numbers in the string is not too important, I would just like the user to be able to type something like either example 1 or 2 if they wish:

字符串中的数字数量不是很重要,我只是希望用户能够输入类似示例 1 或 2 的内容,如果他们愿意:

Example:

例子:

  1. +44 (0) 207 111 1111

  2. 442071111111

  1. +44 (0) 207 111 1111

  2. 442071111111

I have already read through and tested the posted answers to some similar questions to the best of my understanding but so far none of them are working for me the way I want them to.

我已经尽我所能阅读并测试了一些类似问题的已发布答案,但到目前为止,它们都没有按照我希望的方式为我工作。

Please can someone help me out with a clear explanation of how the above should be written for validation?

请有人能帮我清楚地解释一下上面应该如何编写以进行验证吗?

Many thanks to anyone who can help.

非常感谢任何可以提供帮助的人。

回答by Sagar Hirapara

Try this code

试试这个代码

HTML Code

HTML代码

<input type="text" id="phone"/>

JS Code

JS代码

$("#phone").blur(function() {
  var regexp = /^[\s()+-]*([0-9][\s()+-]*){6,20}$/
  var no = $("#phone").val();
  if (!regexp.test(no) && no.length < 0) {
    alert("Wrong phone no");
  }
});

回答by vapcguy

See A comprehensive regex for phone number validation

请参阅电话号码验证的综合正则表达式

Quick cheat sheet

快速备忘单

  • Start the expression: /^
  • If you want to require a space, use: [\s]or \s
  • If you want to require parenthesis, use: [(]and [)]. Using \(and \)is ugly and can make things confusing.
  • If you want anything to be optional, put a ?after it
  • If you want a hyphen, just type -or [-]. If you do not put it first or last in a series of other characters, though, you may need to escape it: \-
  • If you want to accept different choices in a slot, put brackets around the options: [-.\s]will require a hyphen, period, or space. A question mark after the last bracket will make all of those optional for that slot.
  • \d{3}: Requires a 3-digit number: 000-999. Shorthand for [0-9][0-9][0-9].
  • [2-9]: Requires a digit 2-9 for that slot.
  • (\+|1\s)?: Accept a "plus" or a 1 and a space (pipe character, |, is "or"), and make it optional. The "plus" sign must be escaped.
  • If you want specific numbers to match a slot, enter them: [246]will require a 2, 4, or 6. [77|78]will require 77 or 78.
  • $/: End the expression
  • 开始表达: /^
  • 如果您需要一个空间,请使用: [\s]\s
  • 如果您想要求括号,请使用: [(][)]。使用\(and\)是丑陋的,会让事情变得混乱。
  • 如果你想要任何东西是可选的,?在它后面放一个
  • 如果你想要一个连字符,只需输入-[-]。但是,如果您没有将它放在一系列其他字符的第一个或最后一个,则可能需要对其进行转义: \-
  • 如果您想在一个插槽中接受不同的选择,请在选项周围加上括号:[-.\s]将需要一个连字符、句点或空格。最后一个括号后面的问号将使该插槽的所有这些都是可选的。
  • \d{3}:需要 3 位数字:000-999。的简写 [0-9][0-9][0-9]
  • [2-9]:该插槽需要数字 2-9。
  • (\+|1\s)?: 接受一个“加号”或一个 1 和一个空格(管道字符,|, 是“或”),并将其设为可选。“加”号必须转义。
  • 如果您希望特定数字与插槽匹配,请输入它们:[246]将需要 2、4 或 6。 [77|78]将需要 77 或 78。
  • $/: 结束表达式

回答by Ismael Miguel

This is a long regex, but it supports both formats (for example 2 to be a valid international number, is MUST start with either + or 00):

这是一个长正则表达式,但它支持两种格式(例如,2 是有效的国际号码,必须以 + 或 00 开头):

/^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\/]?){0,})(?:[\-\.\ \\/]?(?:#|ext\.?|extension|x)[\-\.\ \\/]?(\d+))?$/i

This allows extensions and a multiple choice of formats and separators.

这允许扩展和多种格式和分隔符的选择。

Matches:

火柴:

  • (+351) 282 43 50 50
  • 90191919908
  • 555-8909
  • 001 6867684
  • 001 6867684x1
  • 1 (234) 567-8901
  • 1-234-567-8901 x1234
  • 1-234-567-8901 ext1234
  • 1-234 567.89/01 ext.1234
  • 1(234)5678901x1234
  • (123)8575973
  • (0055)(123)8575973
  • (+351) 282 43 50 50
  • 90191919908
  • 555-8909
  • 001 6867684
  • 001 6867684x1
  • 1 (234) 567-8901
  • 1-234-567-8901 x1234
  • 1-234-567-8901 分机 1234
  • 1-234 567.89/01 分机 1234
  • 1(234)5678901x1234
  • (123)8575973
  • (0055)(123)8575973

On $n, it saves:

在 $n 上,它节省了:

  1. Country indicator
  2. Phone number
  3. Extention
  1. 国家指标
  2. 电话号码
  3. 扩展

This same answer was given here: A comprehensive regex for phone number validation(direct link to my answer)

这里给出了相同的答案:电话号码验证的综合正则表达式(直接链接到我的答案)

回答by Martin Vilcans

Don't even try.Trying to guard against what you think is invalid input can result in angry users who can't enter perfectly valid phone numbers. And if the user really wants to enter an invalid phone number, he/she will be able to do it anyway.

甚至不要尝试。试图防范您认为无效的输入可能会导致无法输入完全有效电话号码的愤怒用户。如果用户真的想输入一个无效的电话号码,他/她无论如何都可以这样做。

回答by Pank

/*
@isValidUSPhoneFormat function will check valid US Format
    Allowed US Format
(123) 456-7890
123-456-7890
123.456.7890
1234567890
(734) 555.1212
*/   

    function isValidUSPhoneFormat(elementValue){  
            var phoneNumberPattern = /^[(]{0,1}[0-9]{3}[)]{0,1}[-\s.]{0,1}[0-9]{3}[-\s.]{0,1}[0-9]{4}$/;  
            if(phoneNumberPattern.test(elementValue) == false)
            {
                 var phoneNumberPattern = /^(\()?\d{3}(\))?(.|\s)?\d{3}(.|\s)\d{4}$/; 
                 return phoneNumberPattern.test(elementValue);   
            }
            return phoneNumberPattern.test(elementValue);  
        }

May this will help you to understand JavaScript RegEx..

可能这会帮助你理解 JavaScript RegEx ..

回答by shafi7468

function checkPhoneNumber(val) {
    var num = document.getElementById(val).value;
    var mob=/^[+]*[(]{0,1}[0-9]{1,3}[)]{0,1}[-\s\./0-9]*$/g;
    if (mob.test(num) == false) {
        alert("Please Enter Valid Phone Number.");
        document.getElementById(val).value = "";
        return false;
    }
     if (num.length > 15) {
        alert("Only 15 characters allowed for Phone Number field.");
        document.getElementById(val).value = "";
        return false;
    }

    return true;
}

Try it Ones

试试吧