Sure - I’ll intersperse comments throughout the dataITA code that the additional-methods script provides.
/**
* Return true, if the value is a valid date, also making this formal check dd/mm/yyyy.
*
* @example $.validator.methods.date("01/01/1900")
* @result true
*
* @example $.validator.methods.date("01/13/1990")
* @result false
*
* @example $.validator.methods.date("01.01.1900")
* @result false
*
* @example <input name="pippo" class="{dateITA:true}" />
* @desc Declares an optional input element whose value must be a valid date.
*
* @name $.validator.methods.dateITA
* @type Boolean
* @cat Plugins/Validate/Methods
*/
The first line of the comment block gives a good overview of what the below code does. The rest of the comment block gives ideas and details to people using dateITA on other ways that it can be used too.
$.validator.addMethod("dateITA", function(value, element) {
var check = false,
The check variable will be used to record if we think the date is valid or not. Following the standard validation principle, we believe first that it’s false, and only make it true when we know that it is valid.
re = /^\d{1,2}\/\d{1,2}\/\d{4}$/,
This regular expression checks that the date is properly formatted, as nn/nn/nnnn with one of two digits allowed in the day and month areas, but that’s all that it checks. 99/99/9999 is also valid according to the regular expression. So, further checks are needed.
adata, gg, mm, aaaa, xdata;
Those other variables are used throughout the code, and so are declared at at the start here.
if ( re.test(value)) {
If the regular expression test fails, it’s clearly not a valid date.
adata = value.split("/");
gg = parseInt(adata[0], 10);
mm = parseInt(adata[1], 10);
aaaa = parseInt(adata[2], 10);
The above splits up te date in to its three parts. gg for the day, mm for the year, and aaaa for the year. Why is the author using strange variable names such as gg and aaaa? Perhaps he’s from a nation or culture that we don’t know of. Using better named variables such as dd and yyyy would work better though, and make the rest of he code more understandable.
xdata = new Date(aaaa, mm - 1, gg, 12, 0, 0, 0);
if ( ( xdata.getUTCFullYear() === aaaa ) && ( xdata.getUTCMonth () === mm - 1 ) && ( xdata.getUTCDate() === gg ) ) {
Create a date, and check to see if the date values match up with what was entered.
JavaScript date does allow bad date values to be used. For example, if in JavaScript you enter 29/02/2015 and it’s not a leap year, it rolls over to 01/03/2015. Checking that the values match helps to prevent such problems.
The date method also has an interesting little detail where months are given as being from 0 to 11, so we need to remove one from the provided date for that to work properly.
check = true;
} else {
check = false;
}
The date is good or bad, depending on if the dates match or not.
} else {
check = false;
}
When the first regular expression test fails, it’s clearly not a valid date either.
return this.optional(element) || check;
If the date is optional and not required, don’t even bother saying if it’s valid or not.
If it’s not an optional one though and is required, the validated check value is returned.
}, "Please enter a correct date");
This is the error message to provide when the validation check fails.