Skip to content

Instantly share code, notes, and snippets.

@nfhipona
Last active January 19, 2024 03:55
Show Gist options
  • Save nfhipona/43a94d716d48b5f1c1992e5138ca394c to your computer and use it in GitHub Desktop.
Save nfhipona/43a94d716d48b5f1c1992e5138ca394c to your computer and use it in GitHub Desktop.
RegEx Pattern: Phone Number & Card Number Validator

RegEx Pattern: Phone Number & Card Number Validator

Useful for mobile and card number validation with specific format


Phone Number

(((\+91)+(\d{5}|\s){4})|((\d{5}|\s){3})) - will validate phone number with +91 12345 67890 or 12345 67890

(\d{5}|\s) - will validate a group of 5 digits:

`ex-success`: 12345
`ex-fail`   : 12345 67890, ...

(\d{5}|\s){3} - will validate a group of 5 digits with group range of 3 {3} including space:

`ex-success`: 12345 67890
`ex-fail`   : 12345, 12345 67890 12345, ...

(\+91)+(\d{5}|\s){4} - will validate a group of 5 digits and an area code +91 with group range of 4 {4} including spaces:

`ex-success`: +91 12345 67890
`ex-fail`   : +11 12345 67890, +11 12345, +91 12345 67890 12345, ...

Card Number

^((\w{4}\s){3})((\d{4}){1})$ - will validate card number with exact format xxxx xxxx xxxx 1234

`ex-success`: xxxx xxxx xxxx 1234
`ex-success`: aadd 122a xxxx 1234
`ex-fail`   : 1234 aaaa xxxx ab12
`ex-success`: 1234 aaaa xxxx 1234

^((\w{4}\s){3}) - will validate a group of 4 word characters including digits:

`ex-success`: xxxx 1234 xxxx
`ex-fail`   : 12345 ..., aaaa1 xxxx2 ab12 ..., xxxx xxxx xxxx 12345

((\d{4}){1})$ - will look for exact 4 digits at the end

`ex-success`: ... 1234
`ex-fail`   : ... a123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment