OnlineJudge/static/src/js/lib/formValidation/validator/regexp.js
2015-08-02 12:21:12 +08:00

71 lines
1.9 KiB
JavaScript
Executable File

/**
* 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(root, factory) {
"use strict";
// AMD module is defined
if (typeof define === "function" && define.amd) {
define("validator/regexp", ["jquery", "base"], factory);
} else {
// planted over the root!
factory(root.jQuery, root.FormValidation);
}
}(this, function ($, FormValidation) {
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);
}
};
return FormValidation.Validator.regexp;
}));