JavaScript/jQuery VIN 验证器 [英] JavaScript/jQuery VIN Validator

查看:13
本文介绍了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 &amp; 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 验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆