formvalidation
Version:
The best jQuery plugin to validate form fields. Support Bootstrap, Foundation, Pure, SemanticUI, UIKit and custom frameworks
60 lines (56 loc) • 2.45 kB
JavaScript
/**
* uuid validator
*
* @link http://formvalidation.io/validators/uuid/
* @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': {
uuid: {
'default': 'Please enter a valid UUID number',
version: 'Please enter a valid UUID version %s number'
}
}
});
FormValidation.Validator.uuid = {
html5Attributes: {
message: 'message',
version: 'version'
},
/**
* Return true if and only if the input value is a valid UUID string
*
* @see http://en.wikipedia.org/wiki/Universally_unique_identifier
* @param {FormValidation.Base} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Consist of key:
* - message: The invalid message
* - version: Can be 3, 4, 5, null
* @returns {Boolean|Object}
*/
validate: function(validator, $field, options) {
var value = validator.getFieldValue($field, 'uuid');
if (value === '') {
return true;
}
// See the format at http://en.wikipedia.org/wiki/Universally_unique_identifier#Variants_and_versions
var locale = validator.getLocale(),
patterns = {
'3': /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
'4': /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
'5': /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
},
version = options.version ? (options.version + '') : 'all';
return {
valid: (null === patterns[version]) ? true : patterns[version].test(value),
message: options.version
? FormValidation.Helper.format(options.message || FormValidation.I18n[locale].uuid.version, options.version)
: (options.message || FormValidation.I18n[locale].uuid['default'])
};
}
};
}(jQuery));