jQuery Validation插件:提示覆盖

时间:2012-09-12 20:49:08

标签: jquery jquery-plugins validation

我有一个jQuery表单,它有一个排序验证。它是一个数据输入屏幕,上面有两个文本框。每个文本框的大声值为1到无穷大,但第一个控件的“期望”范围为36-84,第二个控制的范围为50-300。如果该值在所需范围内,则提交将通过。如果范围超出了所需范围,但在大声范围内,例如25,则需要在提交后提示用户。如果他们对提示说“是”,则提交通过,如果他们拒绝,则不会。

感谢Cooper Maruyama的帮助,我已经到了试图在ASP.Net世界中实际实现答案的重点。问题是文本框id是在运行时确定的,并传递给id对象中的initialize函数。

当我运行它时,我收到以下错误:无法获取属性'settings'的值:object为null或undefined

这是我的代码:

var manageResidentWeightsJs = {

    initialize: function (ids) {

        var cbDeclined = $(ids.cbDeclined);
        var cbIsOutOfFac = $(ids.cbIsOutOfFac);

        $('<div id=errorSummary>The following values fall out side of the normal range:</div>')
                .append('<ul id="errorsList"></ul>').hide().insertAfter(ids.formView);

        var txtHeight = $(ids.txtHeight);
        var txtWeight = $(ids.txtWeight);

        function confirmation() {
            var x;
            var r = confirm("Are you sure?");
            if (r == true) { return true; }
            else { return false; }
        }

        $(ids.formView).validate({
            invalidHandler: function (form, validator) {
                if (confirmation()) { form.submit(); }
            }
        });

        txtHeight.each(function (index, elem) { $(elem).rules("add", { min: 0 }) });
        txtWeight.each(function (index, elem) { $(elem).rules("add", { min: 0 }) });
    }
}

以下代码用于调用上述代码。在这段代码中,ASP.Net已被'#'取代,以消除问题中的ASP.Net因素:)

$(document).ready(function () {
    var ids = {
      formView: '#mainForm',
      txtHeight: '#heightCtrl',
      txtWeight: '#weightCtrl'
    };

    manageResidentWeightsJs.initialize(ids);
});

1 个答案:

答案 0 :(得分:0)

告诉验证人一个“不受欢迎的”&#39;范围无效&#39;。然后使用invalidHandler显示一个确认框。如果他们打得好,请强制表格提交。

工作示例:http://jsfiddle.net/FqX6h/14/

您必须根据自己的喜好编辑验证规则。

HTML:

 <form id="form" action="">
    <input type="text" name="some_name" value="" id="some_name" minlength="5">
    <input type="submit" value="submit" class="required" size="10" onclick="">
  </form>

JS:

       function confirmation() {
            var r=confirm("Are you sure?");
            if (r==true) {return true;}
            else {return false;}
        }
        $(function() {
            $("#form").validate({
                rules: {
                    some_name: {
                        required: true
                    }
                },
                invalidHandler: function(form, validator) {
                    if (confirmation()) {form.submit();}
                }
            });
        });