Changeset 12675

Show
Ignore:
Timestamp:
02/23/08 23:09:07 (3 months ago)
Author:
peller
Message:

don't do a dojo.indexOf on a hash. Fixes #5956

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • dojox/trunk/validate/creditCard.js

    r9669 r12675  
    1010dojox.validate.isValidCreditCard = function(/*String|Int*/value, /*String*/ccType){ 
    1111        //Summary: 
    12         //  checks if type matches the # scheme, and if Luhn checksum is accurate (unless its an Enroute card, the checkSum is skipped) 
     12        //  checks if ccType matches the # scheme in value, and if Luhn checksum is accurate (unless its an Enroute card, the checkSum is skipped) 
    1313         
    1414        //Value: Boolean 
    15         if(value&&ccType&&((ccType.toLowerCase()=='er'||dojox.validate.isValidLuhn(value))&&(dojox.validate.isValidCreditCardNumber(value,ccType.toLowerCase())))){ 
    16                         return true; //Boolean 
    17         } 
    18         return false; //Boolean 
     15        return ((ccType.toLowerCase() == 'er' || dojox.validate.isValidLuhn(value)) && 
     16                        dojox.validate.isValidCreditCardNumber(value,ccType.toLowerCase())); //Boolean 
    1917} 
    20 dojox.validate.isValidCreditCardNumber = function(/*String|Int*/value,/*String?*/ccType) { 
    21         //Summary: 
    22         //  checks if the # matches the pattern for that card or any card types if none is specified 
    23         //  value == CC #, white spaces and dashes are ignored 
    24         //  ccType is of the values in cardinfo -- if Omitted it it returns a | delimited string of matching card types, or false if no matches found 
    25          
    26         //Value: Boolean 
    27          
    28         if(typeof value!='string'){ 
    29                 value = String(value); 
    30         } 
    31         value = value.replace(/[- ]/g,''); //ignore dashes and whitespaces 
     18 
     19dojox.validate.isValidCreditCardNumber = function(/*String|Int*/value,/*String?*/ccType){ 
     20        // summary: 
     21        //  checks if value matches the pattern for that card or any card types if none is specified 
     22        // 
     23        // value: Boolean 
     24        // CC #, white spaces and dashes are ignored 
     25        // 
     26        // ccType: String? 
     27        // one of the values in cardinfo -- if Omitted it it returns a | delimited string of matching card types, or false if no matches found 
     28 
     29        value = String(value).replace(/[- ]/g,''); //ignore dashes and whitespaces 
     30 
    3231        /*      FIXME: not sure on all the abbreviations for credit cards,below is what each stands for atleast to my knowledge 
    3332                mc: Mastercard 
     
    4140                er: Enroute 
    4241         */ 
     42        var cardinfo = { 
     43                'mc':'5[1-5][0-9]{14}','ec':'5[1-5][0-9]{14}','vi':'4(?:[0-9]{12}|[0-9]{15})', 
     44                'ax':'3[47][0-9]{13}', 'dc':'3(?:0[0-5][0-9]{11}|[68][0-9]{12})', 
     45                'bl':'3(?:0[0-5][0-9]{11}|[68][0-9]{12})','di':'6011[0-9]{12}', 
     46                'jcb':'(?:3[0-9]{15}|(2131|1800)[0-9]{11})','er':'2(?:014|149)[0-9]{11}' 
     47        }; 
     48        if(ccType){ 
     49                var expr = cardinfo[ccType.toLowerCase()]; 
     50                return expr ? !!(value.match(cardinfo[ccType.toLowerCase()])) : false; // boolean 
     51        } 
    4352        var results=[]; 
    44         var cardinfo = { 
    45                 'mc':'5[1-5][0-9]{14}','ec':'5[1-5][0-9]{14}','vi':'4([0-9]{12}|[0-9]{15})', 
    46                 'ax':'3[47][0-9]{13}', 'dc':'3(0[0-5][0-9]{11}|[68][0-9]{12})', 
    47                 'bl':'3(0[0-5][0-9]{11}|[68][0-9]{12})','di':'6011[0-9]{12}', 
    48                 'jcb':'(3[0-9]{15}|(2131|1800)[0-9]{11})','er':'2(014|149)[0-9]{11}' 
    49         }; 
    50         if(ccType&&dojo.indexOf(cardinfo,ccType.toLowerCase())){ 
    51                 return Boolean(value.match(cardinfo[ccType.toLowerCase()])); // boolean 
    52         }else{ 
    53                 for(var p in cardinfo){ 
    54                         if(value.match('^'+cardinfo[p]+'$')!=null){ 
    55                                 results.push(p); 
    56                         } 
     53        for(var p in cardinfo){ 
     54                if(value.match('^'+cardinfo[p]+'$')){ 
     55                        results.push(p); 
    5756                } 
    58                 return (results.length)?results.join('|'):false; // string | boolean 
    59         }        
     57        } 
     58        return results.length ? results.join('|') : false; // string | boolean 
    6059} 
    6160 
     
    8584        var flags = {format:format}; 
    8685        //FIXME? Why does isNumberFormat take an object for flags when its only parameter is either a string or an array inside the object? 
    87         if ((value.length == format.length)&&(dojox.validate.isNumberFormat(value, flags))){ 
    88                 return true; //Boolean 
    89         } 
    90         return false; //Boolean 
     86        //FIXME: um... just check value.length? 
     87        return (value.length == format.length && dojox.validate.isNumberFormat(value, flags)); //Boolean 
    9188}