How to do recursive processing of time and end in form verification

Source: Internet
Author: User

Recursive processing of time starting and ending judgments in form verification

In the most recent project, form validation needs to determine the starting and ending ranges 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, and if it is equal, compare the starting and ending months, and compare the dates if the beginning and ending months are equal. Then, for each validation, the following function can be used 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;
}
}

Thus, at the time of validation, as long as the result returns true it means pass. Such as:

var year = compare (2001,2003, ' years ');
var month = compare (1, 2, ' month ');
var day = compare (12,13, ' days ');
Alert (year && month && day); The result is true------"true"

Revise the starting and ending months and the start and end dates above. Such as:

var year = compare (2001,2003, ' years ');
var month = compare (3,2, ' month ');
var day = compare (24,13, ' days ');
Alert (year && month && day); /result is false------"false"

The execution result, in turn, displays "month", "Day", "false"; in fact, we do not need to validate the date when the start-and-end month is incorrect; the prerequisite for the month verification is the year validation pass; After careful analysis, I decided to store the three parameters of the above function in a single mode, namely:

{
BEGIN:2001,
END:2003,
Error: "The end age must be greater than the starting age"
}

However, I do not want to define the parameters of the function, can the function be automatically validated according to the parameters passed? The answer is yes. At the beginning of the function, the number of parameters is judged first, if greater than 1, the recursive processing is included. 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 as a recursive parameter
}

For arguments, we cannot call the Array.shift () method directly. Although arguments has a length property, but is not an array after all, it is converted to a group using the Array.slice () method. About arguments, you can learn more information on the Internet, here is not to repeat. Why is it only when Len is greater than 1 o'clock to handle it? Because the parameter is 1 o'clock, no next validation is required. When does the recursive processing take place? To think about it, only when the initial value is equal to the end value, and with that in mind, we can easily build our validation 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 code above, Arguments.callee is the function itself, and the use of apply can be used to find relevant information on the Web.

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 as a recursive parameter
}
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;
}
}

The validation function is complete, but you need to be aware that:

    1. Although the number of parameters is not determined, the order of the parameters is important, because the validation of the previous parameter determines whether the next parameter's validation continues;
    2. The second parameter 10 of the parseint () function is not ignored. If omitted, a value starting with 0 (such as 07, 08) will be processed as octal.

It's over, look at the example . Sometimes we don't want to display an error message as alert, and we can customize the handler function to pass it in as the last argument. So at the beginning of the function, get the handler function, such as:

var func = arguments[len-1];
if (typeof func = = ' function ') {
Func (Arguments[0]);
}

So, the final handler function is 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, remaining as recursive 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;
   }
}

Original: http://www.denisdeng.com/?p=631



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.