簡體   English   中英

jQuery驗證與正則表達式提交問題

[英]jquery validation with regular expression submit issue

以下代碼有什么問題? 顯示錯誤消息,但表單已提交。 是由於更新面板,因為控件位於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();

           });
          }

嘗試這個。 我只是刪除了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"
        }
    }
});

更新資料

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM