简体   繁体   English

正则表达式:HTML表单验证中的是否/其他条件

[英]Regex: If/Else Condition in HTML Form Validation

I'm struggling with creating an If/Else statement in regex to a) validate a phone number format, then b) invalidate a certain set of phone numbers. 我正努力在正则表达式中创建If / Else语句,以便a)验证电话号码格式,然后b)使一组特定的电话号码无效。

I currently have: 我目前有:

<input type="tel" name="mobilePhone" pattern="^(?:\+?61|0)4 ?(?:(?:[01] ?[0-9]|2 ?[0-57-9]|3 ?[1-9]|4 ?[7-9]|5 ?[018]) ?[0-9]|3 ?0 ?[0-5])(?: ?[0-9]){5}$">

as a way of targeting the following phone number formats (mobile numbers in Australia): 作为定位以下电话号码格式(澳大利亚的手机号码)的一种方式:

0414570776 0414 570 776 0414570776 0414 570 776 0414570776

However we have a lot of 0411 111 111 or 0400000000 user entries that I would like to try and wash out before submission, but I can't seem to get it right. 但是,我们有很多0411 111 1110400000000用户条目,我希望在提交之前先尝试将其清除,但我似乎无法正确理解。

I want to invalidate a series of numbers, but for this example, these two would suffice: 0411 111 111 or 0400000000 . 我想使一系列数字无效,但对于此示例,这两个就足够了: 0411 111 1110400000000

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thank you 谢谢

Solution

 var re = /^\\s*\\+?04(\\d)(?!\\1\\s*\\1{3}\\s*\\1{3})\\d\\s*\\d{3}\\s*\\d{3}\\s*$/; console.log(re.test('0411111111') ? 'pass' : 'fail', '"0411111111"'); console.log(re.test('0411 111 111') ? 'pass' : 'fail', '"0411 111 111"'); console.log(re.test('0400 000 000') ? 'pass' : 'fail', '"0400 000 000"'); console.log(re.test('0412345678') ? 'pass' : 'fail', '"0412345678"'); console.log(re.test('0412 345 678') ? 'pass' : 'fail', '0412 345 678'); console.log(re.test('+0412345678') ? 'pass' : 'fail', '"+0412345678"'); console.log(re.test('+0412 345 678') ? 'pass' : 'fail', '"+0412 345 678"'); console.log(re.test('0412 345 678') ? 'pass' : 'fail', '"0412 345 678"'); console.log(re.test('+0412 345 678') ? 'pass' : 'fail', '"+0412 345 678"'); console.log(re.test(' +0412 345 678 ') ? 'pass' : 'fail', '" +0412 345 678 "'); 
 <!DOCTYPE> <html> <head></head> <body> <form> <input type='tel' name='mobilePhone' pattern='^\\s*\\+?04(\\d)(?!\\1\\s*\\1{3}\\s*\\1{3})\\d\\s*\\d{3}\\s*\\d{3}\\s*$' placeholder='enter phone number'> <input type='submit'> </form> </body> </html> 

Explanation 说明

  • ^\\s* - starting with zero or more whitespace characters ^\\s* -以零个或多个空白字符开头
  • \\+? - followed by an optional + -随后是可选的+
  • 04 - followed by a literal 04 04随后是文字04
  • (\\d) - followed by a single digit (group 1) (\\d) -后跟一位数字(组1)
  • (?!...) - NOT FOLLOWED BY (?!...) - 不关注

    • \\1 - the value of group 1 \\1组1的值
    • \\s* - followed by zero or more whitespace characters \\s* -后跟零个或多个空格字符
    • \\1{3} - followed by 3 occurrences of the value of group 1 \\1{3} -随后出现3组1的值
    • \\s* - followed by zero or more whitespace characters \\s* -后跟零个或多个空格字符
    • \\1{3} - followed by 3 occurrences of the value of group 1 \\1{3} -随后出现3组1的值
  • \\d - followed by a single digit \\d后跟一位数字

  • \\s* - followed by zero or more whitespace characters \\s* -后跟零个或多个空格字符
  • \\d{3} - followed by 3 occurrences of the value of group 1 \\d{3} -随后出现3组1的值
  • \\s* - followed by zero or more whitespace characters \\s* -后跟零个或多个空格字符
  • \\d{3} - followed by 3 occurrences of the value of group 1 \\d{3} -随后出现3组1的值
  • \\s*$ - ending with zero or more whitespace characters \\s*$ -以零个或多个空白字符结尾

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

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