简体   繁体   English

使用 JavaScript 验证电话号码

[英]Validate phone number with JavaScript

I found this code in some website, and it works perfectly.我在某个网站上找到了这段代码,它运行良好。 It validates that the phone number is in one of these formats:它验证电话号码是以下格式之一:
(123) 456-7890 or 123-456-7890 (123) 456-7890123-456-7890

The problem is that my client (I don't know why, maybe client stuffs) wants to add another format, the ten numbers consecutively, something like this: 1234567890 .问题是我的客户(我不知道为什么,可能是客户的东西)想要添加另一种格式,连续十个数字,如下所示: 1234567890

I'm using this regular expression,我正在使用这个正则表达式,

/^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/

How can I add that it also validates the another format?我如何添加它也验证另一种格式? I'm not good with regular expressions.我不擅长正则表达式。

My regex of choice is:我选择的正则表达式是:

/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im

Valid formats:有效格式:

(123) 456-7890 (123) 456-7890
(123)456-7890 (123)456-7890
123-456-7890 123-456-7890
123.456.7890 123.456.7890
1234567890 1234567890
+31636363634 +31636363634
075-63546725 075-63546725

First off, your format validator is obviously only appropriate for NANP (country code +1) numbers.首先,您的格式验证器显然只适用于NANP (国家代码 +1)数字。 Will your application be used by someone with a phone number from outside North America?电话号码来自北美以外的人是否会使用您的应用程序? If so, you don't want to prevent those people from entering a perfectly valid [international] number.如果是这样,您不想阻止这些人输入完全有效的 [国际] 号码。

Secondly, your validation is incorrect.其次,您的验证不正确。 NANP numbers take the form NXX NXX XXXX where N is a digit 2-9 and X is a digit 0-9. NANP 数字采用NXX NXX XXXX的形式,其中N是数字 2-9, X是数字 0-9。 Additionally, area codes and exchanges may not take the form N11 (end with two ones) to avoid confusion with special services except numbers in a non-geographic area code (800, 888, 877, 866, 855, 900) may have a N11 exchange.此外,区号和交易所不得采用N11形式(以两个 1 结尾)以避免与特殊服务混淆,非地理区号(800、888、877、866、855、900)中的号码可能具有N11交换。

So, your regex will pass the number (123) 123 4566 even though that is not a valid phone number.因此,您的正则表达式将传递号码 (123) 123 4566,即使该号码不是有效的电话号码。 You can fix that by replacing \d{3} with [2-9]{1}\d{2} .您可以通过将\d{3}替换为[2-9]{1}\d{2}来解决此问题。

Finally, I get the feeling you're validating user input in a web browser.最后,我感觉您正在验证 Web 浏览器中的用户输入。 Remember that client-side validation is only a convenience you provide to the user ;请记住,客户端验证只是您为用户提供的一种便利 you still need to validate all input (again) on the server.您仍然需要(再次)验证服务器上的所有输入。

TL;DR don't use a regular expression to validate complex real-world data like phone numbers or URLs . TL;DR不使用正则表达式来验证复杂的真实数据,例如电话号码URL Use a specialized library .使用专门的图书馆

如果您正在寻找 10 位且只有 10 位数字,请忽略除数字之外的所有内容 -

   return value.match(/\d/g).length===10;
/^[+]*[(]{0,1}[0-9]{1,3}[)]{0,1}[-\s\./0-9]*$/g

(123) 456-7890 (123) 456-7890
+(123) 456-7890 +(123) 456-7890
+(123)-456-7890 +(123)-456-7890
+(123) - 456-7890 +(123) - 456-7890
+(123) - 456-78-90 +(123) - 456-78-90
123-456-7890 123-456-7890
123.456.7890 123.456.7890
1234567890 1234567890
+31636363634 +31636363634
075-63546725 075-63546725

This is a very loose option and I prefer to keep it this way, mostly I use it in registration forms where the users need to add their phone number.这是一个非常宽松的选项,我更喜欢保持这种方式,主要是在用户需要添加电话号码的注册表单中使用它。 Usually users have trouble with forms that enforce strict formatting rules, I prefer user to fill in the number and the format it in the display or before saving it to the database.通常用户对强制执行严格格式规则的表单有问题,我更喜欢用户在显示中或在将其保存到数据库之前填写数字和格式。 http://regexr.com/3c53v http://regexr.com/3c53v

What I would do is ignore the format and validate the numeric content:我要做的是忽略格式并验证数字内容:

var originalPhoneNumber = "415-555-1212";

function isValid(p) {
  var phoneRe = /^[2-9]\d{2}[2-9]\d{2}\d{4}$/;
  var digits = p.replace(/\D/g, "");
  return phoneRe.test(digits);
}

The following REGEX will validate any of these formats:以下 REGEX 将验证任何这些格式:

(123) 456-7890 (123) 456-7890
123-456-7890 123-456-7890
123.456.7890 123.456.7890
1234567890 1234567890

/^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/

Where str could be any of these formarts: 555-555-5555 (555)555-5555 (555) 555-5555 555 555 5555 5555555555 1 555 555 5555其中 str 可以是以下任何一种格式: 555-555-5555 (555)555-5555 (555) 555-5555 555 555 5555 5555555555 1 555 555 5555

 function telephoneCheck(str) { var isphone = /^(1\s|1|)?((\(\d{3}\))|\d{3})(\-|\s)?(\d{3})(\-|\s)?(\d{4})$/.test(str); alert(isphone); } telephoneCheck("1 555 555 5555");

I would suggest using something clearer (especially thinking to who will have to maintain the code)... what about:我建议使用更清晰的东西(特别是考虑谁必须维护代码)......怎么样:

var formats = "(999)999-9999|999-999-9999|9999999999";
var r = RegExp("^(" +
               formats
                 .replace(/([\(\)])/g, "\\$1")
                 .replace(/9/g,"\\d") +
               ")$");

where the regexp is built from a clear template ?正则表达式是从一个清晰的模板构建的? Adding a new one would then be a no-brainer and may be even the customer itself could be able to do that in a "options" page.然后添加一个新的将是不费吹灰之力,甚至可能客户本身也可以在“选项”页面中做到这一点。

Try this one - it includes validation for international formats too.试试这个——它也包括对国际格式的验证。

/^[+]?(1\-|1\s|1|\d{3}\-|\d{3}\s|)?((\(\d{3}\))|\d{3})(\-|\s)?(\d{3})(\-|\s)?(\d{4})$/g

This regex validates the following format:此正则表达式验证以下格式:

  • (541) 754-3010 Domestic (541) 754-3010 国内
  • +1-541-754-3010 International +1-541-754-3010 国际
  • 1-541-754-3010 Dialed in the US 1-541-754-3010 在美国拨打
  • 001-541-754-3010 Dialed from Germany 001-541-754-3010 从德国拨出
  • 191 541 754 3010 Dialed from France 191 541 754 3010 从法国拨出

This will work:这将起作用:

/^(()?\d{3}())?(-|\s)?\d{3}(-|\s)?\d{4}$/

The ? ? character signifies that the preceding group should be matched zero or one times.字符表示前面的组应该匹配零次或一次。 The group (-|\s) will match either a - or a |(-|\s)将匹配-| character.特点。 Adding ?添加? after the second occurrence of this group in your regex allows you to match a sequence of 10 consecutive digits.在您的正则表达式中第二次出现该组之后,您可以匹配 10 个连续数字的序列。

try this尝试这个

/^(\+{0,})(\d{0,})([(]{1}\d{1,3}[)]{0,}){0,}(\s?\d+|\+\d{2,3}\s{1}\d+|\d+){1}[\s|-]?\d+([\s|-]?\d+){1,2}(\s){0,}$/gm

valid formats有效格式

  • (123) 456-7890 (123) 456-7890
  • (123)456-7890 (123)456-7890
  • 123-456-7890 123-456-7890
  • 1234567890 1234567890
  • +31636363634 +31636363634
  • +3(123) 123-12-12 +3(123) 123-12-12
  • +3(123)123-12-12 +3(123)123-12-12
  • +3(123)1231212 +3(123)1231212
  • +3(123) 12312123 +3(123) 12312123
  • +3(123) 123 12 12 +3(123) 123 12 12
  • 075-63546725 075-63546725
  • +7910 120 54 54 +7910 120 54 54
  • 910 120 54 54 910 120 54 54
  • 8 999 999 99 99 8 999 999 99 99

https://regex101.com/r/QXAhGV/1 https://regex101.com/r/QXAhGV/1

/^\+?1?\s*?\(?\d{3}(?:\)|[-|\s])?\s*?\d{3}[-|\s]?\d{4}$/

Although this post is an old but want to leave my contribuition.虽然这个帖子很老但是想留下我的贡献。 these are accepted: 5555555555 555-555-5555 (555)555-5555 1(555)555-5555 1 555 555 5555 1 555-555-5555 1 (555) 555-5555这些被接受: 5555555555 555-555-5555 (555)555-5555 1(555)555-5555 1 555 555 5555 1 555-555-5555 1 (555) 555-5555

these are not accepted:这些不被接受:

555-5555 -> to accept this use: ^\+?1?\s*?\(?(\d{3})?(?:\)|[-|\s])?\s*?\d{3}[-|\s]?\d{4}$ 555-5555 -> 接受这种用法: ^\+?1?\s*?\(?(\d{3})?(?:\)|[-|\s])?\s*?\d{3}[-|\s]?\d{4}$

5555555 -> to accept this use: ^\+?1?\s*?\(?(\d{3})?(?:\)|[-|\s])?\s*?\d{3}[-|\s]?\d{4}$ 5555555 -> 接受这种用法: ^\+?1?\s*?\(?(\d{3})?(?:\)|[-|\s])?\s*?\d{3}[-|\s]?\d{4}$

1 555)555-5555 123**&!!asdf# 55555555 (6505552368) 2 (757) 622-7382 0 (757) 622-7382 -1 (757) 622-7382 2 757 622-7382 10 (757) 622-7382 27576227382 (275)76227382 2(757)6227382 2(757)622-7382 (555)5(55?)-5555 1 555)555-5555 123**&!!asdf# 55555555 (6505552368) 2 (757) 622-7382 0 (757) 622-7382 -1 (757) 622-7382 2 757 622-7382 10 (757) 622 -7382 27576227382 (275)76227382 2(757)6227382 2(757)622-7382 (555)5(55?)-5555

this is the code I used:这是我使用的代码:

function telephoneCheck(str) {
  var patt = new RegExp(/^\+?1?\s*?\(?\d{3}(?:\)|[-|\s])?\s*?\d{3}[-|\s]?\d{4}$/);
  return patt.test(str);
}

telephoneCheck("+1 555-555-5555");

Regex正则表达式

Use the following Regex pattern:使用以下正则表达式模式:

/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im

Like this: <input pattern="/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im"> *像这样: <input pattern="/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im"> *


Valid formats:有效格式:

(123) 456-7890, (123) 456-7890,
(123)456-7890, (123)456-7890,
123-456-7890, 123-456-7890,
123.456.7890, 123.456.7890,
1234567890, 1234567890,
+31636363634 or +31636363634 或
075-63546725 075-63546725


真的很简单

"9001234567".match(/^\d{10}$/g)

This reg ex is suitable for international phone numbers and multiple formats of mobile cell numbers.此 reg ex 适用于国际电话号码和多种格式的手机号码。

Here is the regular expression: /^(+{1}\d{2,3}\s?[(]{1}\d{1,3}[)]{1}\s?\d+|+\d{2,3}\s{1}\d+|\d+){1}[\s|-]?\d+([\s|-]?\d+){1,2}$/这是正则表达式:/^(+{1}\d{2,3}\s?[(]{1}\d{1,3}[)]{1}\s?\d+|+\ d{2,3}\s{1}\d+|\d+){1}[\s|-]?\d+([\s|-]?\d+){1,2}$/

Here is the JavaScript function这是JavaScript函数

function isValidPhone(phoneNumber) {
    var found = phoneNumber.search(/^(\+{1}\d{2,3}\s?[(]{1}\d{1,3}[)]{1}\s?\d+|\+\d{2,3}\s{1}\d+|\d+){1}[\s|-]?\d+([\s|-]?\d+){1,2}$/);
    if(found > -1) {
        return true;
    }
    else {
        return false;
    }
}

This validates the following formats:这验证了以下格式:

+44 07988-825 465 (with any combination of hyphens in place of space except that only a space must follow the +44) +44 07988-825 465(用连字符的任意组合代替空格,但只有空格必须跟在 +44 后面)

+44 (0) 7988-825 465 (with any combination of hyphens in place of spaces except that no hyphen can exist directly before or after the (0) and the space before or after the (0) need not exist) +44 (0) 7988-825 465(用连字符的任意组合代替空格,除非连字符不能直接存在于 (0) 之前或之后,并且 (0) 之前或之后的空格不必存在)

123 456-789 0123 (with any combination of hyphens in place of the spaces) 123 456-789 0123(用连字符的任意组合代替空格)

123-123 123 (with any combination of hyphens in place of the spaces) 123-123 123(用连字符的任意组合代替空格)

123 123456 (Space can be replaced with a hyphen) 123 123456(空格可以用连字符代替)

1234567890 1234567890

No double spaces or double hyphens can exist for all formats.所有格式都不能存在双空格或双连字符。

Everyone's answers are great, but here's one I think is a bit more comprehensive...每个人的答案都很棒,但这里有一个我认为更全面的答案......

This is written for javascript match use of a single number in a single line:这是为在单行中使用单个数字的 javascript 匹配而编写的:

^(?!.*911.*\d{4})((\+?1[\/ ]?)?(?![\(\. -]?555.*)\( ?[2-9][0-9]{2} ?\) ?|(\+?1[\.\/ -])?[2-9][0-9]{2}[\.\/ -]?)(?!555.?01..)([2-9][0-9]{2})[\.\/ -]?([0-9]{4})$

If you want to match at word boundaries, just change the ^ and $ to \b如果要在单词边界匹配,只需将 ^ 和 $ 更改为 \b

I welcome any suggestions, corrections, or criticisms of this solution.我欢迎对此解决方案提出任何建议、更正或批评。 As far as I can tell, this matches the NANP format (for USA numbers - I didn't validate other North American countries when creating this), avoids any 911 errors (can't be in the area code or region code), eliminates only those 555 numbers which are actually invalid (region code of 555 followed by 01xx where x = any number).据我所知,这与 NANP 格式相匹配(对于美国号码 - 创建此号码时我没有验证其他北美国家),避免任何 911 错误(不能在区号或地区代码中),消除只有那些实际上无效的 555 个数字(区域代码 555 后跟 01xx,其中 x = 任何数字)。

This function worked for us well:这个功能对我们很有效:

 let isPhoneNumber = input => { try { let ISD_CODES = [93, 355, 213, 1684, 376, 244, 1264, 672, 1268, 54, 374, 297, 61, 43, 994, 1242, 973, 880, 1246, 375, 32, 501, 229, 1441, 975, 591, 387, 267, 55, 246, 1284, 673, 359, 226, 257, 855, 237, 1, 238, 1345, 236, 235, 56, 86, 61, 61, 57, 269, 682, 506, 385, 53, 599, 357, 420, 243, 45, 253, 1767, 1809, 1829, 1849, 670, 593, 20, 503, 240, 291, 372, 251, 500, 298, 679, 358, 33, 689, 241, 220, 995, 49, 233, 350, 30, 299, 1473, 1671, 502, 441481, 224, 245, 592, 509, 504, 852, 36, 354, 91, 62, 98, 964, 353, 441624, 972, 39, 225, 1876, 81, 441534, 962, 7, 254, 686, 383, 965, 996, 856, 371, 961, 266, 231, 218, 423, 370, 352, 853, 389, 261, 265, 60, 960, 223, 356, 692, 222, 230, 262, 52, 691, 373, 377, 976, 382, 1664, 212, 258, 95, 264, 674, 977, 31, 599, 687, 64, 505, 227, 234, 683, 850, 1670, 47, 968, 92, 680, 970, 507, 675, 595, 51, 63, 64, 48, 351, 1787, 1939, 974, 242, 262, 40, 7, 250, 590, 290, 1869, 1758, 590, 508, 1784, 685, 378, 239, 966, 221, 381, 248, 232, 65, 1721, 421, 386, 677, 252, 27, 82, 211, 34, 94, 249, 597, 47, 268, 46, 41, 963, 886, 992, 255, 66, 228, 690, 676, 1868, 216, 90, 993, 1649, 688, 1340, 256, 380, 971, 44, 1, 598, 998, 678, 379, 58, 84, 681, 212, 967, 260, 263], //extract numbers from string thenum = input.match(/[0-9]+/g).join(""), totalnums = thenum.length, last10Digits = parseInt(thenum) % 10000000000, ISDcode = thenum.substring(0, totalnums - 10); //phone numbers are generally of 8 to 16 digits if (totalnums >= 8 && totalnums <= 16) { if (ISDcode) { if (ISD_CODES.includes(parseInt(ISDcode))) { return true; } else { return false; } } else { return true; } } } catch (e) {} return false; } console.log(isPhoneNumber('91-9883208806'));

Validate phone number + return formatted data验证电话号码 + 返回格式化数据

 function validTel(str){ str = str.replace(/[^0-9]/g, ''); var l = str.length; if(l<10) return ['error', 'Tel number length < 10']; var tel = '', num = str.substr(-7), code = str.substr(-10, 3), coCode = ''; if(l>10) { coCode = '+' + str.substr(0, (l-10) ); } tel = coCode +' ('+ code +') '+ num; return ['succes', tel]; } console.log(validTel('+1 [223] 123.45.67'));

/^(()?\d{3}())?(-|\s)?\d{3}(-|\s)?\d{4}$/

The ? ? character signifies that the preceding group should be matched zero or one times.字符表示前面的组应该匹配零次或一次。 The group (-|\s) will match either a - or a |(-|\s)将匹配-| character.特点。

I have to agree that validating phone numbers is a difficult task.我不得不同意验证电话号码是一项艰巨的任务。 As for this specific problem i would change the regex from至于这个具体问题,我会改变正则表达式

/^(()?\d{3}())?(-|\s)?\d{3}(-|\s)\d{4}$/

to

/^(()?\d{3}())?(-|\s)?\d{3}(-|\s)?\d{4}$/

as the only one more element that becomes unnecessary is the last dash/space.因为唯一不再需要的元素是最后一个破折号/空格。

If you using on input tag than this code will help you.如果您在输入标签上使用,则此代码将对您有所帮助。 I write this code by myself and I think this is very good way to use in input.我自己编写了这段代码,我认为这是在输入中使用的非常好的方法。 but you can change it using your format.但您可以使用您的格式更改它。 It will help user to correct their format on input tag.它将帮助用户更正输入标签上的格式。

$("#phone").on('input', function() {  //this is use for every time input change.
        var inputValue = getInputValue(); //get value from input and make it usefull number
        var length = inputValue.length; //get lenth of input

        if (inputValue < 1000)
        {
            inputValue = '1('+inputValue;
        }else if (inputValue < 1000000) 
        {
            inputValue = '1('+ inputValue.substring(0, 3) + ')' + inputValue.substring(3, length);
        }else if (inputValue < 10000000000) 
        {
            inputValue = '1('+ inputValue.substring(0, 3) + ')' + inputValue.substring(3, 6) + '-' + inputValue.substring(6, length);
        }else
        {
            inputValue = '1('+ inputValue.substring(0, 3) + ')' + inputValue.substring(3, 6) + '-' + inputValue.substring(6, 10);
        }       
        $("#phone").val(inputValue); //correct value entered to your input.
        inputValue = getInputValue();//get value again, becuase it changed, this one using for changing color of input border
       if ((inputValue > 2000000000) && (inputValue < 9999999999))
      {
          $("#phone").css("border","black solid 1px");//if it is valid phone number than border will be black.
      }else
      {
          $("#phone").css("border","red solid 1px");//if it is invalid phone number than border will be red.
      }
  });

    function getInputValue() {
         var inputValue = $("#phone").val().replace(/\D/g,'');  //remove all non numeric character
        if (inputValue.charAt(0) == 1) // if first character is 1 than remove it.
        {
            var inputValue = inputValue.substring(1, inputValue.length);
        }
        return inputValue;
}

只是想添加一个专门用于选择非本地电话号码(800 和 900 类型)的解决方案。

(\+?1[-.(\s]?|\()?(900|8(0|4|5|6|7|8)\3+)[)\s]?[-.\s]?\d{3}[-.\s]?\d{4}

Google has a good library for handling phone numbers in Javascript: https://github.com/googlei18n/libphonenumber . Google有一个很好的库来处理JavaScript中的电话号码: https : //github.com/googlei18n/libphonenumber Works also with Java and C++. 也可以与Java和C ++一起使用。

I would prefer using these, because this one must be really well tested in production. 我更喜欢使用这些,因为必须在生产中对它进行真正的测试。 So this should be quite safe to relay on. 因此,这应该很安全。

There are too many regex variants to validate a phone number. 正则表达式的变体太多,无法验证电话号码。 I recommend this article: JavaScript: HTML Form - Phone Number validation , where multiple variants are specified for each case. 我推荐这篇文章: JavaScript:HTML表单-电话号码验证 ,其中为每种情况指定了多种变体。 If you want to deepen in regex for a custom expression, you can review this documentation . 如果您想深入了解自定义表达式的正则表达式,可以查阅本文档

Try this js function.试试这个js函数。 Returns true if it matches and false if it fails Ref如果匹配则返回真,如果失败则返回假Ref

 function ValidatePhoneNumber(phone) { return /^\+?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/.test(phone); }

I took the one provided by @marty and modified it a little bit to accommodate more cases:我采用了@marty 提供的那个,并对其进行了一些修改以适应更多情况:

/^[\+]?([0-9][\s]?|[0-9]?)([(][0-9]{3}[)][\s]?|[0-9]{3}[-\s\.]?)[0-9]{3}[-\s\.]?[0-9]{4,6}$/im

Valid formats:有效格式:

  1. +1 123 4567890 +1 123 4567890
  2. +11234567890 +11234567890
  3. +1(123)4567890 +1(123)4567890
  4. +1(123)456-7890 +1(123)456-7890
  5. +1 (123) 456-7890 +1 (123) 456-7890
  6. +1 (123)456 7890 +1 (123)456 7890
  7. +1 123 456-7890 +1 123 456-7890
  8. +1 123 456 7890 +1 123 456 7890
  9. +1 123-456-7890 +1 123-456-7890
  10. 123-456-7890 123-456-7890
  11. 123 456-7890 123 456-7890
  12. 123 456 7890 123 456 7890
  13. 123 4567890 123 4567890
  14. 1234567890 1234567890

Simple Regular expression: /\b\d{3}[-.]?\d{3}[-.]?\d{4}\b/g简单正则表达式:/\b\d{3}[-.]?\d{3}[-.]?\d{4}\b/ /\b\d{3}[-.]?\d{3}[-.]?\d{4}\b/g

Check out the format, hope it works :检查格式,希望它有效:
444-555-1234 444-555-1234
f:246.555.8888传真:246.555.8888
m:1235554567手机:1235554567

\\(?\d{3}\\)?([\-\s\.])?\d{3}\1?\d{4}

This will validate any phone number of variable format:这将验证任何可变格式的电话号码:

\\(?\d{3}\\)? finds 3 digits enclosed by parenthesis or not.查找是否用括号括起来的 3 位数字。

([\-\s\.])? finds any of these separator characters or not是否找到这些分隔符中的任何一个

\d{3} finds 3 digits \d{3}找到 3 个数字

\1 uses the first matched separator - this ensures that the separators are the same. \1使用第一个匹配的分隔符 - 这确保分隔符相同。 So (000) 999-5555 will not validate here because there is a space and dash separator, so just remove the "\1" and replace with the separator sub-pattern (doing so will also validate non standard formats).因此 (000) 999-5555 将不会在此处验证,因为存在空格和破折号分隔符,因此只需删除“\1”并替换为分隔符子模式(这样做也会验证非标准格式)。 You should however be format hinting for user input anyway.但是,无论如何您都应该对用户输入进行格式提示。

\d{4} finds 4 digits \d{4}找到 4 位数字

Validates:验证:

  • (000) 999 5555 (000) 999 5555
  • (000)-999-5555 (000)-999-5555
  • (000).999.5555 (000).999.5555
  • (000) 999-5555 (000) 999-5555
  • (000)9995555 (000)9995555
  • 000 999 5555 000 999 5555
  • 000-999-5555 000-999-5555
  • 000.999.5555 000.999.5555
  • 0009995555 0009995555

BTW this is for JavaScript hence to double escapes.顺便说一句,这是针对 JavaScript 的,因此是双重转义。

try this尝试这个

/^[\+]?\d{2,}?[(]?\d{2,}[)]?[-\s\.]?\d{2,}?[-\s\.]?\d{2,}[-\s\.]?\d{0,9}$/im

valid formats:有效格式:

  1. (123) 456-7890 (123) 456-7890
  2. (123)456-7890 (123)456-7890
  3. 123-456-7890 123-456-7890
  4. 123.456.7890 123.456.7890
  5. 1234567890 1234567890
  6. +31636363634 +31636363634
  7. +3(123) 123-12-12 +3(123) 123-12-12
  8. +3(123)123-12-12 +3(123)123-12-12
  9. +3(123)1231212 +3(123)1231212
  10. +3(123) 12312123 +3(123) 12312123
  11. 075-63546725 075-63546725

Validate phone number with an indian standard like start with only 6,7,8 or 9 and total digit must be 10 使用印度标准(例如仅以6,7,8或9开头)验证电话号码,且总位数必须为10

The solution is tested and working 该解决方案已经过测试并且可以正常工作

if(phone=='')       
{
    alert("please fill out the field");
}


if ($('#phone').val()!="")
{
    if(!phoneno.test(phone)) 
    {
        alert("enter valid phone number with indian standard.")
        exit;
    }
    else
    {
        alert("valid phone number");
    }
}

A RegExp to Argentina´s numbers: /^((+[0-9]{1,3}))?(([0-9]{8,13})|([0-9]{3,6})-([0-9]{4,7}))$/阿根廷数字的正则表达式:/^((+[0-9]{1,3}))?(([0-9]{8,13})|([0-9]{3,6 })-([0-9]{4,7}))$/

Valids:有效:

X: from 8 to 13 numbers X:从 8 到 13 个数字
  • XXXXXXXX XXXXXXX
  • separate numbers by a hyphen = X from 3 to 6; 用连字符分隔数字 = X 从 3 到 6; Y: from 4 to 7 Y:从 4 到 7
  • XXXX-YYYY XXXX-YYYY
  • Z: from 1 to 3 Z:从 1 到 3
  • (+Z)XXXXXXXX (+Z)XXXXXXXX
  • if you use Z then (+ ) is mandatory. 如果您使用 Z 则 (+ ) 是强制性的。

    Just ignore everything but digits.只是忽略除数字之外的所有内容。

    Here is what I use:这是我使用的:

    // USA phones should contain at least 10 digits. For Ukrainian phones it's OK to have only 9 digits, without leading 0 and country code prefix: [+380] 88 888-88-88.
    String.prototype.isValidPhone = function(digitsCount) {
      var str = this.trim().match(/\d/g);
      return str && str.length >= (digitsCount || 10); // default is at least 10 digits
    }
    

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM