[英]Combine regex for mobile number validation
Scenario: Front end validation - Mobile number Must be numeric, must start with '02', have at least 8? 场景:前端验证-手机号码必须为数字,必须以'02'开头,至少为8? numeric characters, limited to 13?
数字字符,最多13个? numeric characters
数字字符
Examples: 例子:
MSISDN errorMessage 021123 Please enter a valid Vodafone mobile number, 091234567 Please enter a valid Vodafone mobile number, 02112345678910 Please enter a valid Vodafone mobile number, abcdefghijkl Please enter a valid Vodafone mobile number, 021$123456 Please enter a valid Vodafone mobile number MSISDN错误消息021123请输入有效的Vodafone手机号码091234567请输入有效的Vodafone手机号码02112345678910请输入有效的Vodafone手机号码abcdefghijkl请输入有效的Vodafone手机号码021 $ 123456请输入有效的Vodafone手机号码
Also, the format for the number should be 021 055555 when I enter the number in the input field. 另外,当我在输入字段中输入数字时,数字格式应为021 055555。 space after the first three numbers.
前三个数字后的空格。
can anyone help me form a regex for such kind of example? 有人可以帮助我为此类示例形成正则表达式吗?
I believe this regex would perform the match you need: 我相信此正则表达式将执行您需要的匹配:
const numbers = [ "023 45678901", "023 456789012", "023 4567890123", "033 45678901", "013 45678901", "013 45", "013 45678901234567", ] const numberIsValid = number => !!number.match(/02\\d\\s\\d{5,10}/) console.dir(numbers.map(numberIsValid))
Here is what each part of the regex is doing: 这是正则表达式各部分的功能:
02
matches the characters 02 literally (case sensitive) 02
从字面上匹配字符02(区分大小写)
\\d
matches a digit (equal to [0-9]) \\d
匹配一个数字(等于[0-9])
\\s
matches any whitespace character (equal to [\\r\\n\\t\\f\\v ]) \\s
匹配任何空格字符(等于[\\ r \\ n \\ t \\ f \\ v])
\\d{5,10}
matches a digit (equal to [0-9]) \\d{5,10}
匹配一个数字(等于[0-9])
{5,10}
Quantifier — Matches between 5 and 10 times, as many times as possible, giving back as needed (greedy) {5,10}
量词-匹配5到10次,尽可能多地匹配,并根据需要返回(贪婪)
Something of this sort would help 这种东西会有所帮助
input.match(/^02[0-9]{6,11}$/)
Explanation: input is obviously the input. 说明:输入显然是输入。
^
ensures that this is where the string to be matched begins. ^
确保这是要匹配的字符串开始的地方。 [0-9]
means the subsequent characters must be between 0 and 9, and {6-11}
means that these characters can be repeated between 6 and 11 times. [0-9]
表示后续字符必须在0到9之间,而{6-11}
表示这些字符可以重复6到11次。 $
at the end indicates that this is where the string must end. 末尾的
$
表示这是字符串必须结束的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.