JavaScript / jQuery VIN Validator [英] JavaScript/jQuery VIN Validator

查看:124
本文介绍了JavaScript / jQuery VIN Validator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人创建过VIN验证程序?我试图创建一个文本框,用户将输入车辆标识号,然后JS / jQuery将验证如果其正确或不,如果他们键入数字。



我对JS / jQuery非常新,已经找到了一些例子,但是当然没能让他们正常工作...任何一个有任何想法或建议,将非常感谢,特别是如果你可以告诉我如何设置我下面的工作正常!



注意: isVin()函数 cflib.org



HTML:

 < label name =vin> VIN< / label> 
< input type =textname =vin/>

ColdFusion:

 code>< cfscript> 
/ **
*美国车辆识别号(VIN)验证。
*版本1.0由Christopher Jordan
*版本1.1由RHPT,Peter Boughton& Adam Cameron(原函数拒绝有效的VINs)
*
* @param v VIN以验证(必填)
* @return返回布尔值。
* @author Christopher Jordan(cjordan@placs.net)
* @version 1,2013年2月19日
* /
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 = k = 2,1 = 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)##允许评论
^ ##从字符串
##的开头查看http://en.wikipedia.org/wiki/Vehicle_Identification_Number VIN规范
[AZ\d] {3} ##世界制造商标识符(WMI)
[AZ\d] {5} ##车辆描述部分(VDS)
[ \\ dX] ##复选数
[AZ\d] ##模型年
[AZ\d] ##植物
\d {6} ##序列
$ ##到字符串的结尾
;

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];
}
} bb
$ b 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>


解决方案

这里是一个使用正则表达式的客户端解决方案。 / p>

 

 < script src =https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js>< / script>< label name =vin> VIN< / label> ;< input type =textid =vinvalue =1FAFP40634F172825/>< span id =result>< / span>  


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.

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!

Note: The isVin() function is courtesy of cflib.org

HTML:

<label name="vin">VIN</label>
<input type="text" name="vin" />

ColdFusion:

<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-Z\d]{3}            ## World Manufacturer Identifier (WMI)
        [A-Z\d]{5}            ## Vehicle decription section (VDS)
        [\dX]                ## Check digit
        [A-Z\d]                ## Model year
        [A-Z\d]                ## 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>

Test Code:

    <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");
  });
});
  
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 Validator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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