简体   繁体   中英

Javascript: How to validate 4 relative fields to be greater than another

So i have four fields that require some logic.

The logic must be like below, each field has to be greater than the one before. Let the [f1], [f2]... be the fields.

[f1] < [f2] < [f3] < [f4]

I have reached some crazy situation, because what if i have situation like this:

[null] < [null] < [null] < [f4]

And now i add a value to [f4] field. That means that the max value of f3 must be lower than f4 value

Now i add value to field f2

[null] < [f2] < [null] < [f4]

Oops! it also has to be lower than [f4], but if later i enter [f3] value, then it must be lower than [f3]. Same from the other, "GREATER THAN" side...

Is there any simple way to implement this instead of thousand of "if" clauses?

This is how i dynamically change the validation of any field:

  $("form").removeData("validator");
  $("#" + paramID).attr('data-val-range-max', valuemax)
  $("#" + paramID).attr('data-val-range-min', valuemin)
  $.validator.unobtrusive.parse(document);

EDIT:

The valid ending point is whether all of the elements have values or not, the valid endpoind is also like [5] < [null] < [null] < [10]

You can use Array.prototype.every, which test every element and breaks if one does not pass the test.

 function lessThan(array) { var a; array = array.filter(function (a) { // filter the array return a != null; // return only values != null }); a = array.shift(); // get the first element by shifting the array return array.every(function (b) { // return true if every element pass the test var r = a < b; // make the test a = b; // get the new comparing value for the next test return r; // return the test result }); } document.write(lessThan([42]) + '<br>'); // true (obviously!) document.write(lessThan([0, 1, 2, 3]) + '<br>'); // true document.write(lessThan([null, null, null, 4]) + '<br>'); // true document.write(lessThan([null, 3, null, 4]) + '<br>'); // true document.write(lessThan([0, 1, 0, 3]) + '<br>'); // false document.write(lessThan([5, null, null, 4]) + '<br>'); // false document.write(lessThan([null, null, null, null]) + '<br>'); // true 

If you like, you can replace

var r = a < b;
a = b;
return r;

with

return [a < b, a = b][0];

which use a temporary array for storing the test result and assigning the last element to a.

If the purpose is to find if the numbers are in ascendant order:

function lessThan(values){
     if(values.length < 2) throw new Error("Can't make comparison");
     var last = values[0],
         result;

    for(var i = 1; i < values.length; i++){
       result = last < values[i];
       last = values[i];
       if(!result) break;  
    }
    return result;
}

//lessThan([0,1,2,3]) === true;
//lessThan([0,1,0,3]) === false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM