In a recent project, form verification requires a judgment on the start and end range of time: the end time must be greater than or equal to the start time. That is: the end year must be greater than the starting year; if it is equal, compare the starting month with the end month; if the start month is also equal, compare the date. Then, for each verification, you can use the following function to compare.
function compare(begin,end,error){
var begin = parseInt(begin,10);
var end = parseInt(end,10);
var diff = end - begin;
if(diff < 0){
alert(error);
}else{
return true;
}
}
In this way, when verification, as long as the result returns true, it means that it passes. like:
var year = compare(2001,2003,'year');
var month = compare(1,2,'month');
var day = compare(12,13,'day');
alert(year && month && day); //The result is true-------"true"
Change the starting and ending months and start and end dates above. like:
var year = compare(2001,2003,'year');
var month = compare(3,2,'month');
var day = compare(24,13,'day');
alert(year && month && day); /The result is false------"false"
The execution results are displayed in sequence, "month", "day", "false"; In fact, when the start and end months are incorrect, we do not need to verify the date; the prerequisite for month verification is that annual verification is passed ;The prerequisite for day verification is that month verification is passed. After careful analysis, I decided to store the three parameters of the above function in a monolithic mode, namely:
{
begin:2001,
end:2003,
error:"The ending period must be greater than the starting period"
}
However, I don’t want to define the parameters of the function. Can the function be automatically verified based on the passed parameters? The answer is yes. At the beginning of the function, first determine the number of parameters. If it is greater than 1, it contains recursive processing. How to do recursive processing? I did the following inside the function:
var len = arguments.length;
if(len > 1){
var args = Array.prototype.slice.call(arguments);
args.shift(); //Remove the first parameter, and the rest is used as recursive processing parameters
}
For arguments, we cannot call the Array.shift() method directly. Although arguments have a length attribute, they are not an array after all, so use the Array.slice() method to convert it into an array. Regarding arguments, you can learn more about it online, so I will not go into details here. Why is it processed only if len is greater than 1? Because when the parameter is 1, there is no need to perform the next verification step. When will recursive processing be performed? If you think about it carefully, it is only necessary if the initial value is equal to the end value. Once you understand these, it will be easy for us to build our verification function.
var diff = parseInt(arguments[0].end,10) - parseInt(arguments[0].begin,10);
if(diff <0 ){
alert(arguments[0].error);
return false;
}else if(diff == 0){
return len > 1 ? arguments.callee.apply(this,args) : true;
}else{
return true;
}
In the above code, arguments.callee is the function itself. You can find relevant information on the web for the usage of apply.
The verification function has been completed, but it should be noted that:
Although the number of parameters is not determined, the order of parameters is still important, because the verification result of the previous parameter determines whether the verification of the next parameter continues;
The second parameter 10 of the parseInt() function should not be ignored. If ignored, when a value starting with 0 (such as 07, 08) is encountered, it will be processed in octal.
It's over here, look at the examples . Sometimes, we don't want to display error messages in alert mode, we can customize the handler to pass it into it as the last parameter. Then at the beginning of the function, get the processing function, such as:
var func = arguments[len - 1];
if(typeof func == 'function'){
func(arguments[0]);
}
So, the final processing function is like this:
function compare(){
var len = arguments.length;
var func = arguments[len - 1];
if(len > 1){
var args = Array.prototype.slice.call(arguments);
args.shift(); //Remove the first parameter, and the rest is used as recursive processing parameters
}
var diff = parseInt(arguments[0].end,10) - parseInt(arguments[0].begin,10);
if(diff <0 ){
(typeof func == 'function') ? func(arguments[0].error) : alert(arguments[0].error);
return false;
}else if(diff == 0){
return len > 1 ? arguments.callee.apply(this,args) : true;
}else{
return true;
}
}
function compare(){
var len = arguments.length;
if(len > 1){
var args = Array.prototype.slice.call(arguments);
args.shift(); //Remove the first parameter, and the rest is used as recursive processing parameters
}
var diff = parseInt(arguments[0].end,10) - parseInt(arguments[0].begin,10);
if(diff <0 ){
alert(arguments[0].error);
return false;
}else if(diff == 0){
return len > 1 ? arguments.callee.apply(this,args) : true;
}else{
return true;
}
}