user3324415 user3324415 - 24 days ago 6
Javascript Question

Regular expression only 7 or 13 numeric only characters

I am currently using regex:

/^\d+$/
to do numeric only field validation, but I have a need to extend this if possible with the following rules:

Only 7 numeric characters OR 13 numeric characters

Examples:


  • Pass: 1234567

  • Pass: 1234567890123

  • Fail: 12345678

  • Fail: 12345678901234

  • Fail: 12@45!78901234

  • Fail: 12@45!78

  • Fail: 123


Answer

This will work

/^(\d{13}|\d{7})$/