How to validate passport number and id number using javascript?

[code]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 :

A12345674

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 6 numbers.

We bring additional assumptions:

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

My code for HTML
IDENTIFICATION

ID Number




			<input type="radio" name="checkboxSelector" class="checkPassport" ><label>Passport Number</label><br>
			<div class="warn"><br><span id="statusPassport"></span></div>
			<input type="hidden" id="passport" class="checkPassport value=" Value="" />				
			<input type="text" id="passport" class="textPassport" size="9" name="passport" style="display:none" placeholder="Please enter valid passport number" onkeyup="passport_validate(this.value);"></br>
		    <p> <br>

[/code]

My JavaScript code

[code] [/code]

Any help will be appreciated.

Well, I am not good with the regex systems, but, I did find a solution that appears to work.

Perhaps it will for you, too…

http://www.codeproject.com/Questions/1046445/How-to-validate-passport-number-using-javascript

That was my post in codeproject forum.

And, you didn’t read the solution #4 that shows you the code needed? They gave you the correct
regex code for your needs. Just replace yours with theirs. Should work. If it is not, how are you
debugging it?

If you use different usernames on different sites like this one, it is hard for us to know it is you there!

ErnieAlex

I have updated my username to match the one on CodeProject, that’s far enough I guess. Thanks for your suggestion.

But, KingICode, did you look at the list under answer #4 on that post? It gives the steps and code
to solve your problem. I can relist it here if it helps. Their response lists the steps needed and you just
need to use it and I think it solves your passport validation needs. Did you try it?

Yes i did look under that solution, I tried it, but with not luck.

Well, looking at the page they posted,
[1-9]\d\s%3F\d{4}[1-9]%24%2Fig
It looks like it should work for you.

How did you code it? Repost your test code with that in place and tell us where it fails…

I have sort out the problem ErnieAlex, thanx buddy for your time.

I was trying to validate the input element itself, but that i sort it out.

Well, very glad you solved it. Always nice to solve a programming puzzle…

Perhaps you should show the validation code so that others can have it, too. We don’t get many questions
about validating passport numbers here, but, you never know when someone will search for it here.

I will most definitely post the solution.

I manage to fix my error by doing this

HTML CODE

[code]

Passport Number


[/code]

JavaScript CODE

function id_validate(idnumber)

function passport_validate(passport)
{
var regPassport = /^[A-PR-WY][1-9]\d\s?\d{4}[1-9]$/;

	if(regPassport.test(passport) == false){
		document.getElementById("statusPassport").innerHTML = "Passport is not yet valid.";
	}else{
		document.getElementById("statusPassport").innerHTML = "You have entered a valid Passport number!";
	}

}

I was trying to validate the input element itself:

onkeyup=“passport_validate(this);”

when I want to validate its value. Either pass I the value in:

onkeyup=“passport_validate(this.value);”

or just pass this, and pull the value out in your script:

function passport_validate(passport) {
var regPassport =/^[A-PR-WY][1-9]\d\s?\d{4}[1-9]$/;

if (regPassport .test(passport.value) == false) {

Sponsor our Newsletter | Privacy Policy | Terms of Service