简体   繁体   English

正则表达式电话号码验证,使用空格和最小位数的字符

[英]Regular-Expression Phone number validation with spaces and characters with minimun digits

I have less skill for the Regex and I have a quick question. 我对正则表达式的技能较少,但我有一个快速的问题。 I need to validate phone number input with regEx. 我需要使用regEx验证电话号码输入。

Explanation/expression will be really appreciated. 解释/表达将不胜感激。 The Phone numbers can be any of the following formats: 电话号码可以是以下任何一种格式:

(94) 123 345
(94).456.7899
(94)-456-7899
94-456-7899
+94 456 7899
94 456 7899
0094 456 7899
(94) 123
122 3454
1223454

1) Number can include spaces and characters or without characters. 1)数字可以包含空格和字符,也可以不包含字符。
2) It should have minimum 5 digits without spaces and characters. 2)至少5位数字,没有空格和字符。 (I stuck at this point) (我在这里停留)

1st try 第一次尝试

export const basicPhoneNumber = value =>
value && !/^\+?\d+$/i.test(value) 
? 'Invalid phone number' 
: undefined;

2nd try 第二次尝试

  export const basicPhoneNumber = value =>
  value && !/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4,})/i.test(value) 
  ? 'Invalid phone number' 
  : undefined;

Thanks a lot for all your help and have a good one! 非常感谢您的所有帮助,祝您一切顺利!

Phone number validation with regular expression is rather complicated, especially in such cases, yet we can try with an expression similar to: 使用正则表达式进行电话号码验证非常复杂,尤其是在这种情况下,但是我们可以尝试使用类似于以下的表达式:

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

which would likely fail with some desired instances that are not listed, regardless that some inputs such as (000) 000-0000 are not really phone numbers. 无论某些输入(例如(000)000-0000)不是真正的电话号码,都可能因未列出的某些所需实例而失败。

 const regex = /^\\+?\\(?([0-9]{2,4})[)\\s.-]+([0-9]{3,4})([\\s.-]+([0-9]{3,4}))?$/gmi; const str = `(94) 123 345 (94).456.7899 (94)-456-7899 94-456-7899 +94 456 7899 94 456 7899 0094 456 7899 (94) 123 122 3454`; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); } 

Demo 1 演示1

Edit 编辑

For allowing 12234554 , we would make an optional group for ([)\\s.-]+)? 为了允许12234554 ,我们将为([)\\s.-]+)?创建一个可选组([)\\s.-]+)? ,

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

Demo 2 演示2

If all you need to do is to validate the phone numbers with your listed criteria, you can consider stripping out all non-digit characters first, then test with RegExp. 如果您需要做的只是按照列出的标准验证电话号码,则可以考虑先剥离所有非数字字符,然后使用RegExp进行测试。

Not only it is a lot easier, it is also much easier to understand. 这不仅容易得多,而且也容易理解。

 // Sample data const numbers = [ '(94) 123 345', '(94).456.7899', '(94)-456-7899', '94-456-7899', '+94 456 7899', '94 456 7899', '0094 456 7899', '(94) 123', '122 3454' ] // The validation function const validate = value => { value = value.replace(/\\D|(^0+)/g, ''); return /^\\d{5,}$/.test(value); } // Tests numbers.forEach(function(phone){ console.log(validate(phone), `"${phone}"`); }); 

RegExp explanation: RegExp说明:

/\\D|(^0+)/g

  1. \\D matches all non-digit character. \\D匹配所有非数字字符。
  2. (^0+) matches when there is at least 1 leading 0 character(s). (^0+)当至少有1个开头的0个字符时匹配。

/^\\d{5,}$/

Matches when the string starts and ends with a digit, and when the string consists of at least 5 digits, without any non-digits. 当字符串以数字开头和结尾以及字符串包含至少5个数字且没有任何非数字时,匹配。

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

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