如何使用javascript验证护照号码?

编程入门 行业动态 更新时间:2024-10-11 21:28:50
本文介绍了如何使用javascript验证护照号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我做了自己的研究,发现在验证护照号码时遵循了以下指导原则和假设。护照号码的格式如下: 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]有效吗?
  • 最后一个字符无效?
  • 等。
将它们作为级联放入一个函数中最有效的感觉。当人离开输入框时检查值。返回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 the pattern attribute:

<input ... pattern="^[A-PR-WYa-pr-wy][1-9]\d\s?\d{4}[1-9]

更多推荐

如何使用javascript验证护照号码?

本文发布于:2023-11-29 14:02:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646596.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:护照   如何使用   号码   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!