JavaScript/jQuery VIN Validator(JavaScript/jQuery VIN 验证器)
问题描述
有人创建过 VIN 验证器吗?我正在尝试创建一个文本框,用户将在其中输入车辆识别号,然后 JS/jQuery 将验证其是否正确,以防他们输入错误号码.
Has anyone ever created a VIN Validator? I am trying to create a textbox where the user will enter in a Vehicle Identification Number and then JS/jQuery will validate if its correct or not in case they mistype the number.
我对 JS/jQuery 非常陌生,并且找到了一些示例,但当然无法让它们正常工作......任何有任何想法或建议的人将不胜感激,特别是如果你能告诉我如何设置下面的内容才能正常工作!
I am very new to JS/jQuery and have found some examples but of course have not been able to get them to work correctly... Any one with any ideas or suggestions it would be greatly appreciated, especially if you can tell me how to set up what I have below to work properly!
注意:isVin() 函数是由 cflib.org
HTML:
<label name="vin">VIN</label>
<input type="text" name="vin" />
冷融合:
<cfscript>
/**
* US Vehicle Identification Number (VIN) validation.
* version 1.0 by Christopher Jordan
* version 1.1 by RHPT, Peter Boughton & Adam Cameron (original function rejected valid VINs)
*
* @param v VIN to validate (Required)
* @return Returns a boolean.
* @author Christopher Jordan (cjordan@placs.net)
* @version 1, February 19, 2013
*/
function isVIN(v) {
var i = "";
var d = "";
var checkDigit = "";
var sum = 0;
var weights = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2];
var transliterations = {
a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8,
j = 1, k = 2, l = 3, m = 4, n = 5, p = 7, r = 9,
s = 2, t = 3, u = 4, v = 5, w = 6, x = 7, y = 8, z = 9
};
var vinRegex = "(?x) ## allow comments
^ ## from the start of the string
## see http://en.wikipedia.org/wiki/Vehicle_Identification_Number for VIN spec
[A-Zd]{3} ## World Manufacturer Identifier (WMI)
[A-Zd]{5} ## Vehicle decription section (VDS)
[dX] ## Check digit
[A-Zd] ## Model year
[A-Zd] ## Plant
d{6} ## Sequence
$ ## to the end of the string
";
if (! REFindNoCase(vinRegex, arguments.v)) {
return false;
}
for (i = 1; i <= len(arguments.v); i++) {
d = mid(arguments.v, i, 1);
if (! isNumeric(d)) {
sum += transliterations[d] * weights[i];
} else {
sum += d * weights[i];
}
}
checkDigit = sum % 11;
if (checkDigit == 10) {
checkDigit = "x";
}
return checkDigit == mid(arguments.v, 9, 1);
}
</cfscript>
测试代码:
<cfoutput>
<cfset vin = "1GNDM19ZXRB170064">
#vin#: #isVin(vin)#<br />
<cfset vin = "1FAFP40634F172825">
#vin#: #isVin(vin)#<br />
</cfoutput>
推荐答案
这是一个使用正则表达式的客户端解决方案.
Here's a client-side solution using Regular Expressions.
$(function() {
$("#vin").on("keyup blur", function() {
if (validateVin($("#vin").val()))
$("#result").html("That's a VIN");
else
$("#result").html("Not a VIN");
}).trigger("blur");
});
function validateVin(vin) {
var re = new RegExp("^[A-HJ-NPR-Z\d]{8}[\dX][A-HJ-NPR-Z\d]{2}\d{6}$");
return vin.match(re);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label name="vin">VIN</label>
<input type="text" id="vin" value="1FAFP40634F172825" />
<span id="result"></span>
这篇关于JavaScript/jQuery VIN 验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JavaScript/jQuery VIN 验证器
基础教程推荐
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 直接将值设置为滑块 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01