Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for Passport Number

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.

like image 655
Dinny Avatar asked Nov 17 '16 06:11

Dinny


People also ask

What is the format of passport number?

The valid passport number of India must satisfy the following conditions: It should be eight characters long. The first character should be an upper case alphabet. The next two characters should be a number, but the first character should be any number from 1-9 and the second character should be any number from 0-9.

How do I get my passport number?

Enter the Passport Number exactly as it is shown on the title page of your passport, including any letters or numbers pre-printed on the page. If you enter the Passport Number incorrectly, your Visa record will not be found. The Passport Number may only include the letters A to Z and the numbers 0 to 9.

How many numbers are in a passport number?

What does the passport reference number consist of? The passport reference number is a 12-digit code comprising numbers and alphabets.

Which is the passport number in Indian passport?

The passport reference number is a 12 digit unique code that is the combination of alphabets and numerals. The composition of the Passport Reference Number is detailed below, The first four digits of the Passport Reference Number represent the city or the place from where the application is made.


1 Answers

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)

like image 51
Rob Avatar answered Sep 19 '22 22:09

Rob