jquery验证与正则表达式提交问题

时间:2014-01-18 09:49:58

标签: jquery asp.net

以下代码有什么问题?显示错误消息但表单已提交。是否因为控件位于asp.net更新面板内而更新面板。

   $.validator.addMethod("regex",
               function (value, element, regexp) {
                   var re = new RegExp(regexp);
                   return this.optional(element) || re.test(value);
               }


             );

               $("#mainForm").validate({

                   rules: {

                       "<%= txtContractName.UniqueID %>": {
                           required: true,
                        regex: /^[2-9]\d{2}-\d{3}-\d{4}$/
                    }

                },
                   messages: {

                       "<%= txtContractName.UniqueID %>": {
                           regex: "invalid format"
                       }
                   },
                   onsubmit: false,

               });

我解决了这个问题,但另一个问题加剧了。下拉值第二次更改时,它不起作用。好像它只设置了fixedPortion一次。

    <script>
     function fn_init() {
        $.validator.addMethod("regex", function (value, element, regexp) {
            var re = new RegExp(regexp);
            //alert(regexp);
            // return this.optional(element) || What is this line doing
            //alert(re.test(value));
            return re.test(value); // Just test that it passes regex
        }, '');

        //^SPI-[\da-zA-Z''-'\s]{1,10} [a-zA-Z''-'\s]{1,20} \d{4}$
        var fixedPortion = new RegExp("^SPI-" + $("#ddlLOB option:selected").text() + " [\\da-zA-Z''-'\\s]{1,20} \\d{4}$");
       // alert(fixedPortion);
        $("#mainForm").validate({

            rules: {
                "<%= txtContractName.UniqueID %>": {
                    required: true,
                    regex:fixedPortion
                       // regex: /^[2-9]\d{2}-\d{3}-\d{4}$/
                    }

                },
                messages: {

                    "<%= txtContractName.UniqueID %>": {
                        regex: "invalid format"
                    }
                },
            showErrors: function (errorMap, errorList) {

                // Clean up any tooltips for valid elements
                $.each(this.validElements(), function (index, element) {
                    var $element = $(element);

                    $element.data("title", "") // Clear the title - there is no error associated anymore
                        .removeClass("validatorCalloutHighlight");

                });

                // Create new tooltips for invalid elements
                $.each(errorList, function (index, error) {
                    var $element = $(error.element);

                    $element.addClass("validatorCalloutHighlight");
                    //$element.tooltip("destroy") // Destroy any pre-existing tooltip so we can repopulate with new tooltip content
                    //    .data("title", error.message)
                    //    .addClass("error")
                    //    .tooltip(); // Create a new tooltip based on the error messsage we just set in the title
                });
            }


        });
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_initializeRequest(onEachRequest);
        function onEachRequest(sender, args) {
            if ($("#mainForm").valid() == false) {
                args.set_cancel(true);
            }
        }
    }
    </script>

  <script type="text/javascript">
    function pageLoad() {
        $(function () {

            fn_init();

           });
          }

1 个答案:

答案 0 :(得分:0)

试试这个。我刚删除了onsubmit属性。我建议你只测试正则表达式,而不是你之前的任何东西。

$.validator.addMethod("regex", function (value, element, regexp) {
        var re = new RegExp(regexp);
        var value = $('#txtContractName').val();
        // return this.optional(element) || What is this line doing
        return re.test(value); // Just test that it passes regex
    }, '');

$("#mainForm").validate({
    rules: {
        "<%= txtContractName.UniqueID %>": {
            required: true,
            regex: /^[2-9]\d{2}-\d{3}-\d{4}$/
        }
    },
    messages: {
        "<%= txtContractName.UniqueID %>": {
            regex: "invalid format"
        }
    }
});

更新

<强> DEMO