Bug Tracker

Changeset 2252

Show
Ignore:
Timestamp:
07/05/07 19:18:33 (1 year ago)
Author:
joern.zaefferer
Message:

Added vehicle-identification-number method, thanks Chris.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/plugins/validate/additional-methods.js

    r2159 r2252  
    4343    return !jQuery.validator.methods.required(value, element) || /^90[2-5]\d\{2}-\d{4}$/.test(value); 
    4444}, "Your ZIP-code must be in the range 902xx-xxxx to 905-xx-xxxx"); 
     45 
     46jQuery.validator.addMethod( 
     47    "VIN", 
     48    function(v){ 
     49        if ( v.length != 17 ) 
     50            return false; 
     51        var i, n, d, f, cd, cdv; 
     52        var LL = ["A","B","C","D","E","F","G","H","J","K","L","M","N","P","R","S","T","U","V","W","X","Y","Z"]; 
     53        var VL = [1,2,3,4,5,6,7,8,1,2,3,4,5,7,9,2,3,4,5,6,7,8,9]; 
     54        var FL = [8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2]; 
     55        var rs = 0; 
     56        for(i = 0; i < 17; i++){ 
     57            f = FL[i]; 
     58            d = v.slice(i,i+1); 
     59            if(i == 8){ 
     60                cdv = d; 
     61            } 
     62            if(!isNaN(d)){ 
     63                d *= f; 
     64            } 
     65            else{ 
     66                for(n = 0; n < LL.length; n++){ 
     67                    if(d.toUpperCase() === LL[n]){ 
     68                        d = VL[n]; 
     69                        d *= f; 
     70                        if(isNaN(cdv) && n == 8){ 
     71                            cdv = VL[n]; 
     72                        } 
     73                        break; 
     74                    } 
     75                } 
     76            } 
     77            rs += d; 
     78        } 
     79        cd = rs % 11; 
     80        if(cd == 10){cd = "x";} 
     81        if(cd == cdv){return true;} 
     82        return false; 
     83    }, 
     84    "The specified VIN is invalid." 
     85);