简体   繁体   English

Javascript:如何验证4个相对字段大于另一个

[英]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] ...为字段。

[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. 现在,我向[f4]字段添加一个值。 That means that the max value of f3 must be lower than f4 value 这意味着f3的最大值必须小于f4的值

Now i add value to field f2 现在我将值添加到字段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]. 它也必须小于[f4],但是如果以后我输入[f3]值,则它必须小于[f3]。 Same from the other, "GREATER THAN" side... 与另一边一样,“比……更大”

Is there any simple way to implement this instead of thousand of "if" clauses? 有没有简单的方法来实现此功能,而不是成千上万个“ if”子句?

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] 有效的终点是所有元素是否都具有值,有效的端点也像[5] <[null] <[null] <[10]

You can use Array.prototype.every, which test every element and breaks if one does not pass the test. 您可以使用Array.prototype.every,它会测试每个元素,如果没有通过测试则会中断。

 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. 它使用临时数组存储测试结果并将最后一个元素分配给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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何验证大于,小于Javascript? - How to validate greater than, less than in Javascript? 如何通过javascript验证上传的照片不大于1MB - how to validate the photo upload not greater than 1MB from javascript 验证一个字段的值大于php和javascript中另一个字段的值 - validate a field's value is greater than another field's value in php and javascript 如何使用javascript检查值是否大于另一个 - How to check if value is greater than another using javascript 如何检查javaScript中某个数量的数字是否大于另一个数字? - How to check if a number is greater than another number in some quantity in javaScript? 如何测试一个值是否大于数组中的另一个值。 javascript - how to test if a value is greater than another value in an array. javascript 如何验证JavaScript中的3个字段 - How to validate 3 fields in javascript 如何使用JavaScript中的特定关键字来验证格式正确的XML(大于/小于)标签? - How to validate well-formed XML (greater/less than) tags using specific keywords in JavaScript? jQuery或Javascript-验证当前日期是否大于到期日 - Jquery or Javascript - validate if current date is greater than due date Javascript:如何检查数组项是否大于另一个数组中项的两倍 - Javascript: How to check if an array item is greater than twice an item in another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM