Giter Club home page Giter Club logo

parsley.js-credit-card-validator's People

Contributors

gpassarelli avatar rstrydom avatar saevarom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

parsley.js-credit-card-validator's Issues

Accessing the method 'addValidator' through ParsleyValidator is deprecated. Simply call 'window.Parsley.addValidator(...)'

/*

  • Example CVV: data-parsley-cvv
  • Example Creditcard: data-parsley-creditcard
  • Example Creditcard with specific brands: data-parsley-creditcard='visa,mastercard'
    */

//---------------------------------------
// Creditcard validation
//---------------------------------------
(function () {
'use strict';

/**
 * Helper function to get credit card brand name from credit card number.
 * @param  string number        The credit card number
 * @param  boolean check_length Should it check for length
 * @return mixed                The card brand name or null
 */
window.getCreditCardBrand = function(number, check_length){
  var _j, _len1,
  card_name = [],
  number = number.replace(/[ -]/g, '');

  if(!number.length){
    return;
  }

  var check_length = (typeof check_length !== 'undefined') ? check_length : false,
  card_types = [
    {
      name: 'amex',
      pattern: /^3[47]/,
      valid_length: [15]
    },{
      name: 'china_union_pay',
      pattern: /^(62|88)/,
      valid_length: [16, 17, 18, 19]
    },{
      name: 'dankort',
      pattern: /^5019/,
      valid_length: [16]
    },{
      name: 'diners_club_carte_blanche',
      pattern: /^30[0-5]/,
      valid_length: [14]
    },{
      name: 'diners_club_international',
      pattern: /^(30[0-5]|309|36|38|39)/,
      valid_length: [14]
    },{
      name: 'diners_club_us_and_canada',
      pattern: /^(54|55)/,
      valid_length: [16]
    },{
      name: 'discover',
      pattern: /^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)/,
      valid_length: [16]
    },{
      name: 'jcb',
      pattern: /^35(2[89]|[3-8][0-9])/,
      valid_length: [16]
    },{
      name: 'laser',
      pattern: /^(6304|670[69]|6771)/,
      valid_length: [16, 17, 18, 19]
    },{
      name: 'maestro',
      pattern: /^(5018|5020|5038|6304|6759|676[1-3])/,
      valid_length: [12, 13, 14, 15, 16, 17, 18, 19]
    },{
      name: 'mastercard',
      pattern: /^5[1-5]/,
      valid_length: [16]
    },{
      name: 'visa',
      pattern: /^4/,
      valid_length: [16]
    },{
      name: 'visa_electron',
      pattern: /^(4026|417500|4508|4844|491(3|7))/,
      valid_length: [16]
    }
  ];

  for (_j = 0, _len1 = card_types.length; _j < _len1; _j++) {
    var card = card_types[_j];

    if (card.pattern.test(number)) {
      if (check_length && card.valid_length.indexOf(number.length) > -1) {
        card_name.push(card.name);
        break;
      }

      card_name.push(card.name);
    }
  }

  if(card_name.length) {
    return card_name.join(' ');
  }

  return null;
};

//---------------------------------------
// CreditCard Number Verification
//---------------------------------------
window.Parsley.addValidator('creditcard',
    function (value, requirement) {
        var digit, n, _ref2, valid, _j, _len1,
            sum = 0;


        value = value.replace(/[ -]/g, '');
        _ref2 = value.split('').reverse();

        for (n = _j = 0, _len1 = _ref2.length; _j < _len1; n = ++_j) {
            digit = _ref2[n];
            digit = +digit;

            if (n % 2) {
                digit *= 2;

                if (digit < 10) {
                    sum += digit;
                } else {
                    sum += digit - 9;
                }
            } else {
                sum += digit;
            }
        }
        valid =  (sum % 10 === 0);

        // Checks for specific brands
        if(valid && requirement.length){
          var valid_cards = requirement.split(','),
              valid = false,
              card = getCreditCardBrand(value, true).split(' ');

          for( var c in card){
            if(requirement.indexOf(card[c]) > -1) {
              valid = true;
            }
          }
        }

        return valid;
    },32)
    .addMessage('en', 'creditcard', 'This Credit Card number is invalid or this brand is not supported.');


//---------------------------------------
// CCV Verification
//---------------------------------------
window.Parsley.addValidator('cvv',
    function (value) {
        return /^[0-9]{3,4}$/.test(value);
    }, 32)
    .addMessage('en', 'cvv', 'This value should be a valid CVV number');

//---------------------------------------
// Expiry Date Verification
//---------------------------------------
window.Parsley.addValidator('expirydate',
    function (value) {
        var currentTime, expiry, prefix, ref;

        if(value.indexOf('/') === -1){
          return false;
        }

        var date = value.split('/'),
            month = date[0].trim(),
            year  = date[1].trim();

        if (!/^\d+$/.test(month)) {
          return false;
        }
        if (!/^\d+$/.test(year)) {
          return false;
        }
        if (!(parseInt(month, 10) <= 12)) {
          return false;
        }
        if (year.length === 2) {
          prefix = (new Date).getFullYear();
          prefix = prefix.toString().slice(0, 2);
          year = prefix + year;
        }
        expiry = new Date(year, month);
        currentTime = new Date;
        expiry.setMonth(expiry.getMonth() - 1);
        expiry.setMonth(expiry.getMonth() + 1, 1);
        return expiry > currentTime;
    }, 32)
    .addMessage('en', 'expirydate', 'This value should be a valid date');

}());
Accessing the method 'addValidator' through ParsleyValidator is deprecated.
So replace 'window.Parsley.addValidator(...)' with window.ParsleyValidator.addValidator

Some credit card changes

Hi there, there are some upcoming credit card changes coming with Visa having up to 19 digit numbers and some Mastercard numbers starting in the 2 range. Have you got plans to update this library for the upcoming changes?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.