我做了自己的研究,发现在验证护照号码时遵循了以下指导原则和假设。护照号码的格式如下: A12 34567 所有格式均相同护照号码。即第一个字符是英文字母的大写字母,后跟2个数字,然后是可选空格,后跟5个数字。 我们带来额外的假设: 第一个数字或最后一个数字永远不会是0 第一个字符不能是Q,X或Z.
< 输入 type = radio class = checkPassport > < 标签 > Passport数字< / label > < / input > < ; 输入 type = 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]有效吗?
- 最后一个字符无效?
- 等。
这个正则表达式可以解决问题: / ^ [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.
Or, in the pattern attribute:
<input ... pattern="^[A-PR-WYa-pr-wy][1-9]\d\s?\d{4}[1-9]更多推荐
如何使用javascript验证护照号码?
发布评论