formvalidation
Version:
The best jQuery plugin to validate form fields. Support Bootstrap, Foundation, Pure, SemanticUI, UIKit and custom frameworks
96 lines (85 loc) • 4.02 kB
JavaScript
/**
* meid validator
*
* @link http://formvalidation.io/validators/meid/
* @author https://twitter.com/nghuuphuoc
* @copyright (c) 2013 - 2015 Nguyen Huu Phuoc
* @license http://formvalidation.io/license/
*/
(function($) {
FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
'en_US': {
meid: {
'default': 'Please enter a valid MEID number'
}
}
});
FormValidation.Validator.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 {FormValidation.Base} 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 = validator.getFieldValue($field, 'meid');
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.charAt(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 FormValidation.Helper.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
return (sum % 10 === 0)
? (cd === '0')
// 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
: (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;
}
}
};
}(jQuery));