Executing regex statements over object-array with Javascript/jQuery -


i'm looking use regex form manipulation based on user's credit card input (note: front-end validation sake of ux, service , api handles actual credit card validation).

i'd create $watch statement or equivalent match user's input in credit card field against several regex statements ascribed different card types.

so, question in nutshell is: what's best pattern implement can match against multiple regent statements without degrading performance many watchers? first thought write multiple if or switch statements, seems problem extensibility , complicated bit of logic isn't best solution.

thanks everyone!

here's have far:

    var defaultformat = /(\d{1,4})/g;      $scope.cards = [{         type: 'maestro',         pattern: /^(5018|5020|5038|6304|6759|676[1-3])/,         format: defaultformat,         length: [12, 13, 14, 15, 16, 17, 18, 19],         cvclength: [3],         luhn: true     }, {         type: 'dinersclub',         pattern: /^(36|38|30[0-5])/,         format: defaultformat,         length: [14],         cvclength: [3],         luhn: true     }, {         type: 'laser',         pattern: /^(6706|6771|6709)/,         format: defaultformat,         length: [16, 17, 18, 19],         cvclength: [3],         luhn: true     }, {         type: 'jcb',         pattern: /^35/,         format: defaultformat,         length: [16],         cvclength: [3],         luhn: true     }, {         type: 'unionpay',         pattern: /^62/,         format: defaultformat,         length: [16, 17, 18, 19],         cvclength: [3],         luhn: false     }, {         type: 'discover',         pattern: /^(6011|65|64[4-9]|622)/,         format: defaultformat,         length: [16],         cvclength: [3],         luhn: true     }, {         type: 'mastercard',         pattern: /^5[1-5]/,         format: defaultformat,         length: [16],         cvclength: [3],         luhn: true     }, {         type: 'amex',         pattern: /^3[47]/,         format: /(\d{1,4})(\d{1,6})?(\d{1,5})?/,         length: [15],         cvclength: [3, 4],         luhn: true     }, {         type: 'visa',         pattern: /^4/,         format: defaultformat,         length: [13, 16],         cvclength: [3],         luhn: true     }]; 

i have luhn algorithm can use check credit cards.

luhncheck = function(num) { var digit, digits, odd, sum, _i, _len; odd = true; sum = 0; digits = (num + '').split('').reverse(); (_i = 0, _len = digits.length; _i < _len; _i++) {   digit = digits[_i];   digit = parseint(digit, 10);   if ((odd = !odd)) {     digit *= 2;   }   if (digit > 9) {     digit -= 9;   }   sum += digit; } return sum % 10 === 0; }; 

plunker

you're right, you'd want watch value of model in order change "type" stored in variable somewhere.

here's example of relevant watch statement:

    $scope.$watch( 'model', function()     {       var found = false;        angular.foreach( $scope.cards, function( item, index )       {         if ( $scope.model.match( item.pattern ) )         {           $scope.card_type = item.type;           found = true;         }       });        // run luhn method here        if ( !found ) $scope.card_type = 'none';     }); 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -