我正在尝试验证一个人是否超过18岁给她/他的生日,但它不起作用。这是我的代码:
(function ($) {
$.fn.bootstrapValidator.i18n.age = $.extend($.fn.bootstrapValidator.i18n.age || {}, {
'default': 'Please enter a valid birthday'
});
$.fn.bootstrapValidator.validators.age = {
html5Attributes: {
message: 'message',
minage: 'integer',
separator: 'separator'
},
/**
* Return true if the input value is a valid RIF.
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Can consist of the following keys:
* - message: The invalid message
* - minage: A integer number with minimum age
* @returns {Boolean}
*/
validate: function (validator, $field, options) {
var value = $field;
if (value === '') {
return true;
}
// Check wether minage is integer or not
if (!parseInt(options.minage)) {
return false;
}
// Get the birthday
var $date = value.val();
var $split = $date.split(options.separator);
var $dateFullYear = parseInt($split[2], 10);
var $dateMonth = parseInt($split[1], 10);
var $dateDay = parseInt($split[0], 10);
var $tempDate = new Date($dateFullYear + options.minage, $dateMonth, $dateDay);
console.log("$dateFullYear" + $dateFullYear);
console.log("$dateMonth" + $dateMonth);
console.log("$dateDay" + $dateDay);
console.log("$tempDate" + $tempDate);
if ($tempDate <= new Date()) {
return true;
} else {
return false;
}
}
};
}(window.jQuery));
这是我在控制台中获得的内容:
$dateFullYear 2014
$dateMonth 9
$dateDay 1
$tempDate Fri Oct 01 2032 00:00:00 GMT-0430 (Venezuela Standard Time)
我虽然比较错误或我做错了,因为today/now()
日期验证为有效且这是错误的,我的代码周围的任何建议?有帮助吗?
PS:代码是我为BootstrapValidator库
制作的自定义函数的一部分