如何使用javascript验证护照号码? [英] How to validate passport number using javascript ?
问题描述
我做了自己的研究,发现在验证护照号码时遵循了以下指导原则和假设。护照号码的格式如下:
A12 34567
所有格式均相同护照号码。即第一个字符是英文字母的大写字母,后跟2个数字,然后是可选空格,后跟5个数字。
我们带来额外的假设:< br $>
第一个数字或最后一个数字永远不会是0
第一个字符不能是Q,X或Z.
< 输入 type = radio class = checkPassport > < 标签 > Passport数字< / label > < / input > < ; 输入 type < span class =code-keyword> = hidden id = 护照 class = hiddenPassport value = / >
< 输入 类型 = text id = passport class = textPassport name = passport style = display:none 占位符 = 输入您的护照号码 <?php getpostvalue('passport');? > />
我似乎无法获得正确的逻辑,请帮助。
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 = 护照尚未生效。;
}
else
{
document .getElementById ( status)。innerHTML = 您输入了有效的护照号码!;
}
}
小块解决 -
- 删除空格。
- 计算字符 - 是否足够?
- char [0]有效吗?
- 最后一个字符无效?
- 等。
将它们作为级联放入一个函数中最有效的感觉。当人离开输入框时检查值。返回true / false。处理问题,如果有的话。*
*您可以更具体地说明错误,但只是说这是无效的。
这个正则表达式可以解决问题:
/ ^ [A-PR-WY] [1-9] \\\?\\\ {4} [1-9]
/ ig
或者,在模式
属性中:
< 输入 ... pattern = ^ [A-PR-WYa-pr-wy] [1-9] \\\\? \d {4} [1-9]
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.
<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
<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!";
}
}
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.
This regular expression should do the trick:
/^[A-PR-WY][1-9]\d\s?\d{4}[1-9]
/ig
Or, in thepattern
attribute:
<input ... pattern="^[A-PR-WYa-pr-wy][1-9]\d\s?\d{4}[1-9]
这篇关于如何使用javascript验证护照号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!