johnk1
johnk1

Reputation: 247

Javascript Regex to validate string length and last 3 characters as digits

How should I write regex to validate below conditions:

eg: Valid strings: 1234, EC123, 1YC898, 001234

So far, I have tried below regex, but seems like I am missing something?

(^[a-zA-Z0-9]{4,6})?\d{3}$

Upvotes: 1

Views: 4728

Answers (1)

anubhava
anubhava

Reputation: 786146

You can use:

^[a-zA-Z0-9]{1,3}\d{3}$
  • ^[a-zA-Z0-9]{1,3} will match 1 to 3 alpha-numerals at the start
  • \d{3}$ will match 3 digits at the end of your input

Upvotes: 5

Related Questions