简体   繁体   中英

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.

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

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.

I want to invalidate a series of numbers, but for this example, these two would suffice: 0411 111 111 or 0400000000 .

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
  • \\+? - followed by an optional +
  • 04 - followed by a literal 04
  • (\\d) - followed by a single digit (group 1)
  • (?!...) - NOT FOLLOWED BY

    • \\1 - the value of group 1
    • \\s* - followed by zero or more whitespace characters
    • \\1{3} - followed by 3 occurrences of the value of group 1
    • \\s* - followed by zero or more whitespace characters
    • \\1{3} - followed by 3 occurrences of the value of group 1
  • \\d - followed by a single digit

  • \\s* - followed by zero or more whitespace characters
  • \\d{3} - followed by 3 occurrences of the value of group 1
  • \\s* - followed by zero or more whitespace characters
  • \\d{3} - followed by 3 occurrences of the value of group 1
  • \\s*$ - ending with zero or more whitespace characters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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