[英]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.