简体   繁体   中英

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

How should I write regex to validate below conditions:

  • Total length of string should be between 4 to 6
  • Last 3 characters should be digits only.
  • String is only alphanumeric

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}$

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

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