Skip to content

Commit

Permalink
Merge pull request #531 from troymccabe/enhancement-meid
Browse files Browse the repository at this point in the history
Add MEID validator
  • Loading branch information
nghuuphuoc committed Jul 19, 2014
2 parents d669d1f + 5ca8cbb commit 1375348
Show file tree
Hide file tree
Showing 3 changed files with 358 additions and 5 deletions.
86 changes: 86 additions & 0 deletions src/js/validator/meid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
(function($) {
$.fn.bootstrapValidator.i18n.meid = $.extend($.fn.bootstrapValidator.i18n.meid || {}, {
'default': 'Please enter a valid MEID number'
});

$.fn.bootstrapValidator.validators.meid = {
/**
* Validate MEID (Mobile Equipment Identifier)
* Examples:
* - Valid: 293608736500703710, 29360-87365-0070-3710, AF0123450ABCDE, AF-012345-0ABCDE
* - Invalid: 2936087365007037101
*
* @see http://en.wikipedia.org/wiki/Mobile_equipment_identifier
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Can consist of the following keys:
* - message: The invalid message
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (value === '') {
return true;
}

switch (true) {
// 14 digit hex representation (no check digit)
case /^[0-9A-F]{15}$/i.test(value):
// 14 digit hex representation + dashes or spaces (no check digit)
case /^[0-9A-F]{2}[- ][0-9A-F]{6}[- ][0-9A-F]{6}[- ][0-9A-F]$/i.test(value):
// 18 digit decimal representation (no check digit)
case /^\d{19}$/.test(value):
// 18 digit decimal representation + dashes or spaces (no check digit)
case /^\d{5}[- ]\d{5}[- ]\d{4}[- ]\d{4}[- ]\d$/.test(value):
// grab the check digit
var cd = value[value.length - 1];

// strip any non-hex chars
value = value.replace(/[- ]/g, '');

// if it's all digits, luhn base 10 is used
if (value.match(/^\d*$/i)) {
return $.fn.bootstrapValidator.helpers.luhn(value);
}

// strip the check digit
value = value.slice(0, -1);

// get every other char, and double it
var cdCalc = '';
for (var i = 1; i <= 13; i += 2) {
cdCalc += (parseInt(value.charAt(i), 16) * 2).toString(16);
}

// get the sum of each char in the string
var sum = 0;
for (i = 0; i < cdCalc.length; i++) {
sum += parseInt(cdCalc.charAt(i), 16);
}

// if the last digit of the calc is 0, the check digit is 0
if (sum % 10 == 0) {
return cd == 0;
} else {
// subtract it from the next highest 10s number (64 goes to 70)
// and subtract the sum
// double it and turn it into a hex char
return cd == ((Math.floor((sum + 10) / 10) * 10 - sum) * 2).toString(16);
}

// 14 digit hex representation (no check digit)
case /^[0-9A-F]{14}$/i.test(value):
// 14 digit hex representation + dashes or spaces (no check digit)
case /^[0-9A-F]{2}[- ][0-9A-F]{6}[- ][0-9A-F]{6}$/i.test(value):
// 18 digit decimal representation (no check digit)
case /^\d{18}$/.test(value):
// 18 digit decimal representation + dashes or spaces (no check digit)
case /^\d{5}[- ]\d{5}[- ]\d{4}[- ]\d{4}$/.test(value):
return true;

default:
return false;
}
}
};
}(window.jQuery));
144 changes: 139 additions & 5 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3561,20 +3561,154 @@ describe('lessThan', function() {
});
});

describe('meid', function() {
beforeEach(function () {
$([
'<form class="form-horizontal" id="meidForm">',
'<div id="msg"></div>',
'<div class="form-group">',
'<input type="text" name="meid" data-bv-meid />',
'</div>',
'</form>'
].join('\n')).appendTo('body');

$('#meidForm').bootstrapValidator();

this.bv = $('#meidForm').data('bootstrapValidator');
this.$meid = this.bv.getFieldElements('meid');
});

afterEach(function () {
$('#meidForm').bootstrapValidator('destroy').remove();
});

it('Valid MEID (14 hex, check digit)', function() {
this.bv.resetForm();
this.$meid.val('A00000049259B16');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (14 hex, dashes, check digit)', function() {
this.bv.resetForm();
this.$meid.val('A0-000004-9259B1-6');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (14 hex, spaces, check digit)', function() {
this.bv.resetForm();
this.$meid.val('A0 000004 9259B1 6');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (18 dec, check digit)', function() {
this.bv.resetForm();
this.$meid.val('2936087365007037100');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (18 dec, dashes, check digit)', function() {
this.bv.resetForm();
this.$meid.val('29360-87365-0070-3710-0');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (18 dec, spaces, check digit)', function() {
this.bv.resetForm();
this.$meid.val('29360 87365 0070 3710 0');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (14 hex)', function() {
this.bv.resetForm();
this.$meid.val('AF0123450ABCDE');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (14 hex, dashes)', function() {
this.bv.resetForm();
this.$meid.val('AF-012345-0ABCDE');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (14 hex, spaces)', function() {
this.bv.resetForm();
this.$meid.val('AF 012345 0ABCDE');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (18 dec)', function() {
this.bv.resetForm();
this.$meid.val('293608736500703710');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (18 dec, dashes)', function() {
this.bv.resetForm();
this.$meid.val('29360-87365-0070-3710');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (18 dec, spaces)', function() {
this.bv.resetForm();
this.$meid.val('29360 87365 0070 3710');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Invalid MEID (14 hex, bad check digit)', function() {
this.bv.resetForm();
this.$meid.val('A00000049259B15');
this.bv.validate();
expect(this.bv.isValid()).toBeFalsy();
});

it('Invalid MEID (13 hex)', function() {
this.bv.resetForm();
this.$meid.val('A00000049259B');
this.bv.validate();
expect(this.bv.isValid()).toBeFalsy();
});

it('Invalid MEID (18 dec, bad check digit)', function() {
this.bv.resetForm();
this.$meid.val('2936087365007037101');
this.bv.validate();
expect(this.bv.isValid()).toBeFalsy();
});

it('Invalid MEID (17 dec)', function() {
this.bv.resetForm();
this.$meid.val('29360873650070371');
this.bv.validate();
expect(this.bv.isValid()).toBeFalsy();
});
});

describe('uri', function() {
beforeEach(function () {
$([
'<form class="form-horizontal" id="uriForm">',
'<div id="msg"></div>',
'<div class="form-group">',
'<input type="text" name="uri" data-bv-uri />',
'</div>',
'<div id="msg"></div>',
'<div class="form-group">',
'<input type="text" name="uri" data-bv-uri />',
'</div>',
'</form>'
].join('\n')).appendTo('body');

$('#uriForm').bootstrapValidator();

this.bv = $('#uriForm').data('bootstrapValidator');
this.bv = $('#uriForm').data('bootstrapValidator');
this.$uri = this.bv.getFieldElements('uri');
});

Expand Down
133 changes: 133 additions & 0 deletions test/spec/validator/meid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
describe('meid', function() {
beforeEach(function () {
$([
'<form class="form-horizontal" id="meidForm">',
'<div id="msg"></div>',
'<div class="form-group">',
'<input type="text" name="meid" data-bv-meid />',
'</div>',
'</form>'
].join('\n')).appendTo('body');

$('#meidForm').bootstrapValidator();

this.bv = $('#meidForm').data('bootstrapValidator');
this.$meid = this.bv.getFieldElements('meid');
});

afterEach(function () {
$('#meidForm').bootstrapValidator('destroy').remove();
});

it('Valid MEID (14 hex, check digit)', function() {
this.bv.resetForm();
this.$meid.val('A00000049259B16');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (14 hex, dashes, check digit)', function() {
this.bv.resetForm();
this.$meid.val('A0-000004-9259B1-6');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (14 hex, spaces, check digit)', function() {
this.bv.resetForm();
this.$meid.val('A0 000004 9259B1 6');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (18 dec, check digit)', function() {
this.bv.resetForm();
this.$meid.val('2936087365007037100');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (18 dec, dashes, check digit)', function() {
this.bv.resetForm();
this.$meid.val('29360-87365-0070-3710-0');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (18 dec, spaces, check digit)', function() {
this.bv.resetForm();
this.$meid.val('29360 87365 0070 3710 0');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (14 hex)', function() {
this.bv.resetForm();
this.$meid.val('AF0123450ABCDE');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (14 hex, dashes)', function() {
this.bv.resetForm();
this.$meid.val('AF-012345-0ABCDE');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (14 hex, spaces)', function() {
this.bv.resetForm();
this.$meid.val('AF 012345 0ABCDE');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (18 dec)', function() {
this.bv.resetForm();
this.$meid.val('293608736500703710');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (18 dec, dashes)', function() {
this.bv.resetForm();
this.$meid.val('29360-87365-0070-3710');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Valid MEID (18 dec, spaces)', function() {
this.bv.resetForm();
this.$meid.val('29360 87365 0070 3710');
this.bv.validate();
expect(this.bv.isValid()).toBeTruthy();
});

it('Invalid MEID (14 hex, bad check digit)', function() {
this.bv.resetForm();
this.$meid.val('A00000049259B15');
this.bv.validate();
expect(this.bv.isValid()).toBeFalsy();
});

it('Invalid MEID (13 hex)', function() {
this.bv.resetForm();
this.$meid.val('A00000049259B');
this.bv.validate();
expect(this.bv.isValid()).toBeFalsy();
});

it('Invalid MEID (18 dec, bad check digit)', function() {
this.bv.resetForm();
this.$meid.val('2936087365007037101');
this.bv.validate();
expect(this.bv.isValid()).toBeFalsy();
});

it('Invalid MEID (17 dec)', function() {
this.bv.resetForm();
this.$meid.val('29360873650070371');
this.bv.validate();
expect(this.bv.isValid()).toBeFalsy();
});
});

0 comments on commit 1375348

Please sign in to comment.