72 lines
2.1 KiB
JavaScript
Executable File
72 lines
2.1 KiB
JavaScript
Executable File
/**
|
|
* numeric validator
|
|
*
|
|
* @link http://formvalidation.io/validators/numeric/
|
|
* @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/numeric", ["jquery", "base"], factory);
|
|
} else {
|
|
// planted over the root!
|
|
factory(root.jQuery, root.FormValidation);
|
|
}
|
|
|
|
}(this, function ($, FormValidation) {
|
|
FormValidation.I18n = $.extend(true, FormValidation.I18n || {}, {
|
|
'en_US': {
|
|
numeric: {
|
|
'default': 'Please enter a valid float number'
|
|
}
|
|
}
|
|
});
|
|
|
|
FormValidation.Validator.numeric = {
|
|
html5Attributes: {
|
|
message: 'message',
|
|
separator: 'separator'
|
|
},
|
|
|
|
enableByHtml5: function($field) {
|
|
return ('number' === $field.attr('type')) && ($field.attr('step') !== undefined) && ($field.attr('step') % 1 !== 0);
|
|
},
|
|
|
|
/**
|
|
* Validate decimal number
|
|
*
|
|
* @param {FormValidation.Base} validator The validator plugin instance
|
|
* @param {jQuery} $field Field element
|
|
* @param {Object} options Consist of key:
|
|
* - message: The invalid message
|
|
* - separator: The decimal separator. Can be "." (default), ","
|
|
* @returns {Boolean}
|
|
*/
|
|
validate: function(validator, $field, options) {
|
|
if (this.enableByHtml5($field) && $field.get(0).validity && $field.get(0).validity.badInput === true) {
|
|
return false;
|
|
}
|
|
|
|
var value = validator.getFieldValue($field, 'numeric');
|
|
if (value === '') {
|
|
return true;
|
|
}
|
|
var separator = options.separator || '.';
|
|
if (separator !== '.') {
|
|
value = value.replace(separator, '.');
|
|
}
|
|
|
|
return !isNaN(parseFloat(value)) && isFinite(value);
|
|
}
|
|
};
|
|
|
|
|
|
return FormValidation.Validator.numeric;
|
|
}));
|