Sha256: e6e160e899ad1c96b196e0a4db94377c1e62583f2296428bd735e691aabbd2e8

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

var ico_validator = {
  valid_ico: function(value) {
    return value.length === 8 && value.match(/^\d+$/) && ico_validator.last_number_valid(value);
  },
  last_number_valid: function(value) {
    return parseInt(value[7], 10) === ico_validator.calculate_valid_last_number(value);
  },
  calculate_valid_last_number: function(value) {
    var sum = 0;
    for( var i = 0; i <= 6; i++ ) {
      sum += value[i] * (8 - i);
    }
    var mod = sum % 11;
    if( mod === 0 || mod === 10 ) {
      return 1;
    }
    if( mod === 1 ) {
      return 0;
    }
    return 11 - mod;
  }
};

jQuery( function($) {
  var ico_fields = $('.ico-validate');

  function validate(el) {
    var value = $(el).val();
    if( value.length <= 0 || ico_validator.valid_ico(value) ) {
      $(el).parent().removeClass('field_with_errors').removeClass('error');
    } else {
      $(el).parent().addClass('field_with_errors').addClass('error');
    }
  }

  ico_fields.on('change', function() {
    validate(this);
  });

  ico_fields.on('keyup', function() {
    validate(this);
  });

  ico_fields.closest('form').on('submit', function() {
    var wrong = $(this).find('.field_with_errors .ico-validate');
    if( wrong.length <= 0 ) {
      return;
    }
    wrong.focus();
    return false;
  });
});

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ico-validator-0.2.1 app/assets/javascripts/ico-validator.js