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

How should I write regex to validate below conditions:Total length of string should be between 4 to 6L

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

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}$
Share Improve this question asked Jul 21, 2014 at 17:35 johnk1johnk1 2478 silver badges19 bronze badges 2
  • debuggex. is great for trying your pattern and seeing why it works, or not, when you change it. The question is: what are you really trying to match. Is this homework with nonsense strings, or are you actually trying to find real strings from real data that need to match a specific pattern? – Mike 'Pomax' Kamermans Commented Jul 21, 2014 at 17:37
  • Thanks for the link Mike. I am using regexr.. This is for one of my SAP UI5 application. Need to set a validation regex for one of the input field. – johnk1 Commented Jul 21, 2014 at 17:48
Add a ment  | 

1 Answer 1

Reset to default 5

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745652613a4638354.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信