Click here to Skip to main content
15,921,577 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I did my own research and find out that following guidelines and assumptions are followed while validating Passport Numbers. The Passport Number is of the following format :

A12 34567

The format will be same for all the Passport numbers. i.e. the first character is an upper case letter of the English alphabet, followed by 2 numbers, then an optional space followed by 5 numbers.

We bring additional assumptions:

The first digit or the last digit will never be 0
The first character cannot be Q, X or Z.

HTML
<input type="radio"  class="checkPassport"><label>Passport Number</label></input><input type="hidden"  id="passport" class="hiddenPassport" value="" />
<input type="text" id="passport" class="textPassport" name="passport" style="display:none" placeholder="Enter your passport number"  <?php getpostvalue('passport');?> />


I cant seem to get the right logic for this, please help.

JavaScript code
JavaScript
<pre lang="Javascript">
function said_validate(passport)
{
        var regsaid = /[a-zA-Z]{2}[0-9]{7}/;
 
        if(regsaid.test(passport) == false)
        {
            document.getElementById("status").innerHTML = "Passport is not yet valid.";
        }
        else
        {
            document.getElementById("status").innerHTML = "You have entered a valid Passport number!";
        }
}
Posted
Updated 3-Nov-15 3:57am
v3
Comments
Debojyoti Saha 3-Nov-15 8:45am    
use regex for validation
iDoKin 3-Nov-15 8:52am    
Can you please give me the regex for passport validation.

Solve in small pieces -
  • Remove spaces.

  • Count characters - are there enough?

  • Is char[0] valid?

  • is last char invalid?

  • etc.


Put them in a function as a cascade that makes the most efficient sense. Check the value when the person leaves the input box. Return true/false. Handle problems if any.*



* You can be more specific about the error but just saying it's invalid is enough.

 
Share this answer
 
This regular expression should do the trick:
/^[A-PR-WY][1-9]\d\s?\d{4}[1-9]$/ig

Or, in the pattern attribute:
HTML
<input ... pattern="^[A-PR-WYa-pr-wy][1-9]\d\s?\d{4}[1-9]$" />

  • ^: Start of line;
  • [A-PR-WY]: One of A-P, R-W or Y - in other words, A-Z excluding Q, X and Z;
  • [1-9]: Any digit except 0, no repetitions;
  • \d: Any digit, no repetitions;
  • \s?: Zero or one white-space characters;
  • \d{4}: Exactly four digits;
  • [1-9]: Any digit except 0, no repetitions;
  • $: End of line;


http://refiddle.com/refiddles/5638cd4075622d334e050000[^]
http://regexper.com/#%2F^[A-PR-WY][1-9]\d\s%3F\d{4}[1-9]%24%2Fig[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 3-Nov-15 11:27am    
5ed.
—SA
iDoKin 5-Nov-15 2:26am    
Thanks for the pattern Richard.

*What about the ig after the last forward slash?

ig - means what?

Just what to be sure about it.
Richard Deeming 5-Nov-15 7:52am    
Those are the flags for the expression:
Advanced searching with flags | Regular Expressions | Javascript | MDN[^]
JavaScript RegExp Reference | w3schools[^]

i means the pattern is case-insensitive. That way, you don't have to provide both upper- and lower-case ranges for the first character.
g means "find all matches" - it probably isn't needed here, as you're matching the entire string.
iDoKin 5-Nov-15 7:53am    
Okay, Thanks a lot. :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900