forked from stevenmills/bootstrapvalidator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #531 from troymccabe/enhancement-meid
Add MEID validator
- Loading branch information
Showing
3 changed files
with
358 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |