Jquery 验证器动态参数

发布于 2024-10-01 18:30:25 字数 863 浏览 0 评论 0原文

有没有办法将动态参数传递到自定义 jquery 验证方法中?具体来说,我正在寻找一种方法来比较 2 个控件,并希望将一个控件传递给另一个控件的验证方法进行比较。

这是我目前所拥有的:

 //Add method
 $.validator.addMethod("lowerInt", function(value, element, params) {
            alert('inside method');
            if (isNaN(parseInt(params)))
                return true;
            else
                return parseInt(value) < parseInt(params);
        }, $.validator.format("Please enter a number smaller than {0}")); 

 //Set validation
 $("#form1").validate({
     rules: {
        lowInt: {
            required: true,
            integer: true,
            lowerInt: 8 //I would like to pass a dynamic value here
        }
     }
 });

如果我按照上面的方式运行它,它就会按预期工作。如果我将传递给 lowerInt 的 8 更改为 $('#highInt').val(),则似乎只为 lowerInt 函数设置一次值,并且永远不会更新它。我意识到我可以通过调用 $('#highInt').val() 来获取方法内部的值,但如果可能的话,我想传递该值。

Is there a way to pass dynamic parameters into a custom jquery validate method? Specifically, I was looking for a way to compare 2 controls and would like to pass one to the other's validate method for comparison.

Here is what I currently have:

 //Add method
 $.validator.addMethod("lowerInt", function(value, element, params) {
            alert('inside method');
            if (isNaN(parseInt(params)))
                return true;
            else
                return parseInt(value) < parseInt(params);
        }, $.validator.format("Please enter a number smaller than {0}")); 

 //Set validation
 $("#form1").validate({
     rules: {
        lowInt: {
            required: true,
            integer: true,
            lowerInt: 8 //I would like to pass a dynamic value here
        }
     }
 });

If I run it as above, it works as it should. If I change the 8 passed in to lowerInt to $('#highInt').val(), it seems to set the value for the lowerInt function only once and never update it. I realize that I can get the value inside of the method by calling $('#highInt').val(), but I would like to pass the value instead if at all possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-10-08 18:30:25

我会传入一个选择器,如下所示:

//Add method
$.validator.addMethod("lowerInt", function(value, element, params) {
  var hi = typeof params[0] == "string" ? 
           parseInt($(params[0]).val(), 10) :
           params[0];
  if (isNaN(hi))
    return true;
  else
    return parseInt(value) < hi;
}, $.validator.format("Please enter a number smaller than {0}")); 

//Set validation
$("#form1").validate({
  rules: {
    lowInt: {
        required: true,
        integer: true,
        lowerInt: '#highInt'
    }
  }
});

此版本将采用数字选择器,使其尽可能灵活。

I would pass in a selector, like this:

//Add method
$.validator.addMethod("lowerInt", function(value, element, params) {
  var hi = typeof params[0] == "string" ? 
           parseInt($(params[0]).val(), 10) :
           params[0];
  if (isNaN(hi))
    return true;
  else
    return parseInt(value) < hi;
}, $.validator.format("Please enter a number smaller than {0}")); 

//Set validation
$("#form1").validate({
  rules: {
    lowInt: {
        required: true,
        integer: true,
        lowerInt: '#highInt'
    }
  }
});

This version will take either a number or a selector, making it as flexible as possible.

铃予 2024-10-08 18:30:25

将参数设为函数。

$("#form1").validate({
     rules: {
        lowInt: {
            required: true,
            integer: true,
            lowerInt: function(){ return $('#highInt').val(); }
        }
     }
 });

然后你在验证方法中调用该函数,如下所示

var high = params[0]();

Make the parameter a function.

$("#form1").validate({
     rules: {
        lowInt: {
            required: true,
            integer: true,
            lowerInt: function(){ return $('#highInt').val(); }
        }
     }
 });

Then you call the function in the validate method something like

var high = params[0]();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文