Regex for Passport Number

China☆狼群 提交于 2020-01-21 12:30:08

问题


I am trying to implement regex validation for passport number.

My requirement is

  1. Length should be minimum 3 characters to a maximum of 20 characters.
  2. Should not be only 0's

I tried with the below regular expression

^(?!0{3,20})[a-zA-Z0-9]{3,20}$

This seems to work for most of the cases, but incase my text contains 3 or more leading zeros, it seems to fail. Such as '00000001'.

Example Matches

  • 101AE24 - Working as expected
  • 00 - Not working as expected
  • 01234 - Working as expected
  • 00001 - Not working (But it should also be match)

Any help would be much appreciated.


回答1:


What about this?

^(?!^0+$)[a-zA-Z0-9]{3,20}$

Instead of saying 'reject values with 3-20 zeros', we can just say 'reject anything that only contains zeros (regardless of the length, since <3 and >20 fail anyway)



来源:https://stackoverflow.com/questions/40647728/regex-for-passport-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!