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

66 lines
2.1 KiB
JavaScript
Executable File

/**
* grid validator
*
* @link http://formvalidation.io/validators/grid/
* @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/grid", ["jquery", "base"], factory);
} else {
// planted over the root!
factory(root.jQuery, root.FormValidation);
}
}(this, function ($, FormValidation) {
FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
'en_US': {
grid: {
'default': 'Please enter a valid GRId number'
}
}
});
FormValidation.Validator.grid = {
/**
* Validate GRId (Global Release Identifier)
* Examples:
* - Valid: A12425GABC1234002M, A1-2425G-ABC1234002-M, A1 2425G ABC1234002 M, Grid:A1-2425G-ABC1234002-M
* - Invalid: A1-2425G-ABC1234002-Q
*
* @see http://en.wikipedia.org/wiki/Global_Release_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, 'grid');
if (value === '') {
return true;
}
value = value.toUpperCase();
if (!/^[GRID:]*([0-9A-Z]{2})[-\s]*([0-9A-Z]{5})[-\s]*([0-9A-Z]{10})[-\s]*([0-9A-Z]{1})$/g.test(value)) {
return false;
}
value = value.replace(/\s/g, '').replace(/-/g, '');
if ('GRID:' === value.substr(0, 5)) {
value = value.substr(5);
}
return FormValidation.Helper.mod37And36(value);
}
};
return FormValidation.Validator.grid;
}));