简体   繁体   English

电话号码正则表达式验证

[英]Phone number regex validation

Need to validate phone numbers which are separated by comma and it can have country code as start parameter. 需要验证以逗号分隔的电话号码,并且可以将国家/地区代码作为起始参数。 I need to do temporary validation in front end side such as validating different numbers 我需要在前端进行临时验证,例如验证不同的数字

  1. number with country code(min length and max length should be specified): +919845098450 带国家/地区代码的号码(应指定最小长度和最大长度):+ 919845098450
  2. number without country code(min length and max length should be specified):9845098450 没有国家代码的号码(应指定最小长度和最大长度):9845098450

and these numbers are separated by comma(+919845098450,9845098450 etc) so for i have done it for single phone number . 这些数字用逗号(+919845098450,9845098450等)分隔,所以我已经为单个电话号码做了。

^([+])?[0-9()\-#+]*$

and comma separated 和逗号分开

^([+])?[,\d{9}]*$

but the above regex is not showing error when the input ends with comma (+919845098450,9845098450,). 但输入以逗号结尾 ,上述正则表达式不显示错误 (+919845098450,9845098450,)。

I need to do it for comma separated numbers in jquery/javascript. 我需要在jquery / javascript中为逗号分隔的数字执行此操作。

if possible i need to specify min and max length with and without country code. 如果可能的话,我需要指定有和没有国家代码的最小和最大长度。 your help is appreciated. 感谢您的帮助。

You may try this, 你可以试试这个,

/^(?:\+\d{2})?\d{10}(?:,(?:\+\d{2})?\d{10})*$/.test(str);

DEMO DEMO

  • (?:\\+\\d{2})? helps to match an optional country code. 有助于匹配可选的国家/地区代码。
  • \\d{10} matches the 10 digit number. \\d{10}匹配10位数字。
  • (?:,(?:\\+\\d{2})?\\d{10})* helps to match the following zero or more phone numbers. (?:,(?:\\+\\d{2})?\\d{10})*有助于匹配以下零个或多个电话号码。

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

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