formvalidation
Version:
The best jQuery plugin to validate form fields. Support Bootstrap, Foundation, Pure, SemanticUI, UIKit and custom frameworks
55 lines (49 loc) • 1.63 kB
JavaScript
/**
* regexp validator
*
* @link http://formvalidation.io/validators/regexp/
* @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': {
regexp: {
'default': 'Please enter a value matching the pattern'
}
}
});
FormValidation.Validator.regexp = {
html5Attributes: {
message: 'message',
regexp: 'regexp'
},
enableByHtml5: function($field) {
var pattern = $field.attr('pattern');
if (pattern) {
return {
regexp: pattern
};
}
return false;
},
/**
* Check if the element value matches given regular expression
*
* @param {FormValidation.Base} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Consists of the following key:
* - regexp: The regular expression you need to check
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var value = validator.getFieldValue($field, 'regexp');
if (value === '') {
return true;
}
var regexp = ('string' === typeof options.regexp) ? new RegExp(options.regexp) : options.regexp;
return regexp.test(value);
}
};
}(jQuery));