有没有一种方法可以插入 return `bool` 并会在 jQuery Validate pipeline 中调用的独立函数?

Is there a way to plug in standalone function that would return `bool` and would be called in jQuery Validate pipeline?

我正在使用 MVC、jQuery 和 jQuery 验证插件。

我在使用 jQuery 验证验证时可见或不可见的字段集时遇到问题。

如果客户选择 radiobutton 他有其他地址,则会显示邮政编码字段。该字段需要验证器,可以正常工作。但是,当用户输入邮政编码时,他必须单击 Find Address 按钮,然后才会进行 ajax 呼叫并显示该邮政编码的地址。然后用户必须单击地址并选择它。

我遇到的问题:

  1. 当用户输入邮政编码并提交表单而不单击 Find Address 验证通过,因为已通过所需的验证。

  2. 当用户输入邮政编码并单击 Find Address 但未从列表中选择地址并提交时,表单验证通过。

有一些不可见的字段正在填充地址,因此我可以通过从忽略列表中删除 then 来取消忽略那些要验证的特定字段

ignore: ":hidden, :hidden:not(#HiddenAddressFields)"

但这是有条件的,所以在我要注册的验证方法中

$.validator.addMethod("mainAddressValidator", function() {
   ...
});

如果验证这些字段,我将不得不执行检查逻辑,而且这可能会导致副作用并且隐藏字段列表很长。我不喜欢这个的另一个原因是因为我们正在使用捆绑这将 'leak' 到所有其他页面。

有没有一种方法可以插入 return bool 并在 JQuery 不依赖于元素的表单验证管道中调用的独立函数可见(或与此相关的任何东西)?

HTML(不是我认为这有帮助,但无论如何都会有人要求)

<div class="form-group" id="IsMainResidence">
    <div class="col-md-12 field-wrapper">
<label class="" for="IsMainResidence">Do you wish for correspondence to be sent to this address?</label>        <div class="row">
            <div class="controls">
                <div class="col-md-6 col-sm-6 col-xs-9 radio-align fake-radio">
                    <div class="radio inline">                                  
                        <input checked="checked" class="tooltip" data-val="true" data-val-required="Please select whether the property is your main residence" id="IsMainResidencetrue" name="IsMainResidence" type="radio" value="True"><label for="IsMainResidencetrue"><strong>Yes</strong></label>
                    </div>
                    <div class="radio inline">
                        <input class="tooltip" id="IsMainResidencefalse" name="IsMainResidence" type="radio" value="False"><label for="IsMainResidencefalse"><strong>No</strong></label>
                    </div>
                </div>
                <div class="col-md-6  col-sm-6  col-xs-3">
                        <span class="valid-tick hidden-sm hidden-xs tick-validation-valid"></span>
                                        <img alt="help" class="tooltip" id="help-IsMainResidence" src="/legal-services/Client/images/home/help-button.png">             </div>
            </div>
<span class="field-validation-valid" data-valmsg-for="IsMainResidence" data-valmsg-replace="true"></span>           <div class="tooltip-container"></div>
        </div>
    </div>
</div>
<div id="home-address-section" style="display:none;">
<input id="MainResidenceAddressSection_IsIncluded" name="MainResidenceAddressSection.IsIncluded" type="hidden" value="False">
    <!--Home address details start-->
    <div id="MainResidenceDetails">
        <div id="MainResidenceAddressErrorMessage" class="error-group-container" style="display: none;">
            <p class="error-group-text"></p>
        </div>

        <div id="MainResidenceAddressSearch" style="margin-bottom: 20px; ">
            <div class="form-group">
    <div class="col-md-12">
        <label class="" for="MainResidenceAddressSection_PostcodeForSearch">What is your postcode?</label>
        <div class="row">
            <div class="controls">
                <div class="col-md-6 col-sm-8 col-xs-9">
                    <input class="tooltip form-control" data-val="true" data-val-regex="Please enter a valid postcode" data-val-regex-pattern="^(([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$)" data-val-required="Please enter your postcode" data-val-restrictedpostcode="Sorry, we do not operate in the postcode provided" data-val-restrictedpostcode-postcodelist="AB1,AB2,AB3,AB4,AB5,AB6,AB7,AB8,AB9,AB10,AB11,AB12,AB13,AB14,AB15,AB16,AB17,AB18,AB19,AB20,AB21,AB22,AB23,AB24,AB25,AB26,AB27,AB28,AB29,AB30,AB31,AB32,AB33,AB34,AB35,AB36,AB37,AB38,AB39,AB40,AB41,AB42,AB43,AB44,AB45,AB46,AB47,AB48,AB49,AB50,AB51,AB52,AB53,AB54,AB55,AB56,AB57,AB58,AB59,AB60,AB61,AB62,AB63,AB64,AB65,AB66,AB67,AB68,AB69,AB70,AB71,AB72,AB73,AB74,AB75,AB76,AB77,AB78,AB79,AB80,AB81,AB82,AB83,AB84,AB85,AB86,AB87,AB88,AB89,AB90,AB91,AB92,AB93,AB94,AB95,AB96,AB97,AB98,AB99,BT1,BT2,BT3,BT4,BT5,BT6,BT7,BT8,BT9,BT10,BT11,BT12,BT13,BT14,BT15,BT16,BT17,BT18,BT19,BT20,BT21,BT22,BT23,BT24,BT25,BT26,BT27,BT28,BT29,BT30,BT31,BT32,BT33,BT34,BT35,BT36,BT37,BT38,BT39,BT40,BT41,BT42,BT43,BT44,BT45,BT46,BT47,BT48,BT49,BT50,BT51,BT52,BT53,BT54,BT55,BT56,BT57,BT58,BT59,BT60,BT61,BT62,BT63,BT64,BT65,BT66,BT67,BT68,BT69,BT70,BT71,BT72,BT73,BT74,BT75,BT76,BT77,BT78,BT79,BT80,BT81,BT82,BT83,BT84,BT85,BT86,BT87,BT88,BT89,BT90,BT91,BT92,BT93,BT94,DD1,DD2,DD3,DD4,DD5,DD6,DD7,DD8,DD9,DD10,DD11,DG1,DG2,DG3,DG4,DG5,DG6,DG7,DG8,DG9,DG10,DG11,DG12,DG13,DG14,DG15,DG16,EH1,EH2,EH3,EH4,EH5,EH6,EH7,EH8,EH9,EH10,EH11,EH12,EH13,EH14,EH15,EH16,EH17,EH18,EH19,EH20,EH21,EH22,EH23,EH24,EH25,EH26,EH27,EH28,EH29,EH30,EH31,EH32,EH33,EH34,EH35,EH36,EH37,EH38,EH39,EH40,EH41,EH42,EH43,EH44,EH45,EH46,EH47,EH48,EH49,EH50,EH51,EH52,EH53,EH54,EH55,EH56,EH57,EH58,EH59,EH60,EH61,EH62,EH63,EH64,EH65,EH66,EH67,EH68,EH69,EH70,EH71,EH72,EH73,EH74,EH75,EH76,EH77,EH78,EH79,EH80,EH81,EH82,EH83,EH84,EH85,EH86,EH87,EH88,EH89,EH90,EH91,EH92,EH93,EH94,EH95,EH96,EH97,EH98,EH99,FK1,FK2,FK3,FK4,FK5,FK6,FK7,FK8,FK9,FK10,FK11,FK12,FK13,FK14,FK15,FK16,FK17,FK18,FK19,FK20,FK21,GY1,GY2,GY3,GY4,GY5,GY6,GY7,GY8,GY9,GY10,G1,G2,G3,G4,G5,G6,G7,G8,G9,G10,G11,G12,G13,G14,G15,G16,G17,G18,G19,G20,G21,G22,G23,G24,G25,G26,G27,G28,G29,G30,G31,G32,G33,G34,G35,G36,G37,G38,G39,G40,G41,G42,G43,G44,G45,G46,G47,G48,G49,G50,G51,G52,G53,G54,G55,G56,G57,G58,G59,G60,G61,G62,G63,G64,G65,G66,G67,G68,G69,G70,G71,G72,G73,G74,G75,G76,G77,G78,G79,G80,G81,G82,G83,G84,G85,G86,G87,G88,G89,G90,HS1,HS2,HS3,HS4,HS5,HS6,HS7,HS8,HS9,IM1,IM2,IM3,IM4,IM5,IM6,IM7,IM8,IM9,IM86,IM87,IM99,IV1,IV2,IV3,IV4,IV5,IV6,IV7,IV8,IV9,IV10,IV11,IV12,IV13,IV14,IV15,IV16,IV17,IV18,IV19,IV20,IV21,IV22,IV23,IV24,IV25,IV26,IV27,IV28,IV29,IV30,IV31,IV32,IV33,IV34,IV35,IV36,IV37,IV38,IV39,IV40,IV41,IV42,IV43,IV44,IV45,IV46,IV47,IV48,IV49,IV50,IV51,IV52,IV53,IV54,IV55,IV56,IV57,IV58,IV59,IV60,IV61,IV62,IV63,IV64,IV65,IV66,IV67,IV68,IV69,IV70,IV71,IV72,IV73,IV74,IV75,IV76,IV77,IV78,IV79,IV80,IV81,IV82,IV83,IV84,IV85,IV86,IV87,IV88,IV89,IV90,IV91,IV92,IV93,IV94,IV95,IV96,IV97,IV98,IV99,JE1,JE2,JE3,JE4,JE5,KA1,KA2,KA3,KA4,KA5,KA6,KA7,KA8,KA9,KA10,KA11,KA12,KA13,KA14,KA15,KA16,KA17,KA18,KA19,KA20,KA21,KA22,KA23,KA24,KA25,KA26,KA27,KA28,KA29,KA30,KW1,KW2,KW3,KW4,KW5,KW6,KW7,KW8,KW9,KW10,KW11,KW12,KW13,KW14,KW15,KW16,KW17,KY1,KY2,KY3,KY4,KY5,KY6,KY7,KY8,KY9,KY10,KY11,KY12,KY13,KY14,KY15,KY16,KY99,ML1,ML2,ML3,ML4,ML5,ML6,ML7,ML8,ML9,ML10,ML11,ML12,PA1,PA2,PA3,PA4,PA5,PA6,PA7,PA8,PA9,PA10,PA11,PA12,PA13,PA14,PA15,PA16,PA17,PA18,PA19,PA20,PA21,PA22,PA23,PA24,PA25,PA26,PA27,PA28,PA29,PA30,PA31,PA32,PA33,PA34,PA35,PA36,PA37,PA38,PA39,PA40,PA41,PA42,PA43,PA44,PA45,PA46,PA47,PA48,PA49,PA50,PA51,PA52,PA53,PA54,PA55,PA56,PA57,PA58,PA59,PA60,PA61,PA62,PA63,PA64,PA65,PA66,PA67,PA68,PA69,PA70,PA71,PA72,PA73,PA74,PA75,PA76,PA77,PA78,PA79,PA80,PA81,PA82,PA83,PA84,PA85,PA86,PA87,PA88,PA89,PA90,PA91,PA92,PA93,PA94,PA95,PA96,PA97,PA98,PH1,PH2,PH3,PH4,PH5,PH6,PH7,PH8,PH9,PH10,PH11,PH12,PH13,PH14,PH15,PH16,PH17,PH18,PH19,PH20,PH21,PH22,PH23,PH24,PH25,PH26,PH27,PH28,PH29,PH30,PH31,PH32,PH33,PH34,PH35,PH36,PH37,PH38,PH39,PH40,PH41,PH42,PH43,PH44,PH45,PH46,PH47,PH48,PH49,PH50,TD1,TD2,TD3,TD4,TD5,TD6,TD7,TD8,TD9,TD10,TD11,TD12,TD13,TD14,TD15,ZE1,ZE2,ZE3" id="MainResidenceAddressSection-PostcodeForSearch" maxlength="4096" name="MainResidenceAddressSection.PostcodeForSearch" placeholder="" type="text" value="">
                </div>
                <div class="col-md-6  col-sm-4  col-xs-3">
                    <span class="valid-tick hidden-sm hidden-xs"></span>
                    <img alt="help" class="tooltip" id="help-MainResidenceAddressSection-PostcodeForSearch" src="/legal-services/Client/images/home/help-button.png">               </div>
            </div>
            <span class="field-validation-valid" data-valmsg-for="MainResidenceAddressSection.PostcodeForSearch" data-valmsg-replace="true"></span> 
            <div class="tooltip-container"></div>

        </div>
    </div>
</div>
            <div class="summary-group-bottom-buttons clearfix"><a class="button-green-medium" data-address-finder-url="/legal-services/AddressLookup/FindAddress" data-postcode-search-url="/legal-services/AddressLookup/Find" id="FindHomeAddress"><span>Find Address</span></a></div>
        </div>          

        <!--Address summary-->
        <div id="MainResidenceAddressSummary" class="summary-group no-margin-top" style="display: none">
            <h3>Your main residence address</h3>
            <div class="summary-text">
                <p id="MainResidenceHousenameSummary"><strong>House name:</strong> <span></span></p>
                <p id="MainResidenceHousenumberSummary"><strong>House number:</strong> <span></span></p> 
                <p id="MainResidenceStreetSummary"><strong>Street:</strong> <span></span></p> 
                <p id="MainResidenceTownSummary"><strong>Town:</strong> <span></span></p> 
                <p id="MainResidenceCountySummary"><strong>County:</strong> <span></span></p> 
                <p id="MainResidencePostcodeSummary"><strong>Postcode:</strong> <span></span></p>  
            </div>

            <div id="MainResidenceSummaryEdit" class="summary-edit">
                <a class="blue-outline-button-small"><span>Edit</span></a>
            </div>
        </div>

        <!--Address list--> 
        <div id="MainResidenceAddressList" class="address-list-wrapper clearfix" style="margin-top: 20px; display: none;">
            <ul>                
            </ul>
            <p class="pull-left"><a href="#" id="EnterDifferentHomePostcode" class="">Enter a different postcode</a></p>
            <p class="pull-right"><a href="#" id="MainResidenceAddressNotListed" class="">My address is not listed</a></p>
        </div>



        <!--Address lookup-->
        <div id="MainResidenceAddessLookup" style="margin-top: 20px; display: none;">
        <div class="form-group">
    <div class="col-md-12">
        <label class="" for="MainResidenceAddressSection_HouseName">House name</label>
        <div class="row">
            <div class="controls">
                <div class="col-md-6 col-sm-8 col-xs-9">
                    <input class="tooltip form-control" data-val="true" data-val-atleastonerequired="Please provide either your house name or house number" data-val-atleastonerequired-inputstocheck="MainResidenceAddressSection-HouseNumber,MainResidenceAddressSection-HouseName" data-val-length="Please enter a valid house name with no more than 30 characters" data-val-length-max="30" id="MainResidenceAddressSection-HouseName" maxlength="30" name="MainResidenceAddressSection.HouseName" placeholder="" type="text" value="">
                </div>
                <div class="col-md-6  col-sm-4  col-xs-3">
                    <span class="valid-tick hidden-sm hidden-xs"></span>
                                    </div>
            </div>
            <span class="field-validation-valid" data-valmsg-for="MainResidenceAddressSection.HouseName" data-valmsg-replace="true"></span> 
            <div class="tooltip-container"></div>

        </div>
    </div>
</div>
        <div class="form-group">
    <div class="col-md-12">
        <label class="" for="MainResidenceAddressSection_HouseNumber">House number</label>
        <div class="row">
            <div class="controls">
                <div class="col-md-6 col-sm-8 col-xs-9">
                    <input class="tooltip form-control" id="MainResidenceAddressSection-HouseNumber" maxlength="4096" name="MainResidenceAddressSection.HouseNumber" placeholder="" type="text" value="">
                </div>
                <div class="col-md-6  col-sm-4  col-xs-3">
                    <span class="valid-tick hidden-sm hidden-xs"></span>
                                    </div>
            </div>
            <span class="field-validation-valid" data-valmsg-for="MainResidenceAddressSection.HouseNumber" data-valmsg-replace="true"></span>   
            <div class="tooltip-container"></div>

        </div>
    </div>
</div>
        <div class="form-group">
    <div class="col-md-12">
        <label class="" for="MainResidenceAddressSection_Street">Street</label>
        <div class="row">
            <div class="controls">
                <div class="col-md-6 col-sm-8 col-xs-9">
                    <input class="tooltip form-control" data-val="true" data-val-length="Please enter your valid street name with no more than 30 characters" data-val-length-max="30" data-val-required="Please enter your street name" id="MainResidenceAddressSection-Street" maxlength="30" name="MainResidenceAddressSection.Street" placeholder="" type="text" value="">
                </div>
                <div class="col-md-6  col-sm-4  col-xs-3">
                    <span class="valid-tick hidden-sm hidden-xs"></span>
                                    </div>
            </div>
            <span class="field-validation-valid" data-valmsg-for="MainResidenceAddressSection.Street" data-valmsg-replace="true"></span>    
            <div class="tooltip-container"></div>

        </div>
    </div>
</div>
        <div class="form-group">
    <div class="col-md-12">
        <label class="" for="MainResidenceAddressSection_Town">Town</label>
        <div class="row">
            <div class="controls">
                <div class="col-md-6 col-sm-8 col-xs-9">
                    <input class="tooltip form-control" data-val="true" data-val-length="Please enter a valid town name with no more than 30 characters" data-val-length-max="30" data-val-required="Please enter the town" id="MainResidenceAddressSection-Town" maxlength="30" name="MainResidenceAddressSection.Town" placeholder="" type="text" value="">
                </div>
                <div class="col-md-6  col-sm-4  col-xs-3">
                    <span class="valid-tick hidden-sm hidden-xs"></span>
                                    </div>
            </div>
            <span class="field-validation-valid" data-valmsg-for="MainResidenceAddressSection.Town" data-valmsg-replace="true"></span>  
            <div class="tooltip-container"></div>

        </div>
    </div>
</div>
        <div class="form-group">
    <div class="col-md-12">
        <label class="" for="MainResidenceAddressSection_County">County</label>
        <div class="row">
            <div class="controls">
                <div class="col-md-6 col-sm-8 col-xs-9">
                    <input class="tooltip form-control" data-val="true" data-val-length="Please enter a valid county name with no more than 30 characters" data-val-length-max="30" id="MainResidenceAddressSection-County" maxlength="30" name="MainResidenceAddressSection.County" placeholder="" type="text" value="">
                </div>
                <div class="col-md-6  col-sm-4  col-xs-3">
                    <span class="valid-tick hidden-sm hidden-xs"></span>
                                    </div>
            </div>
            <span class="field-validation-valid" data-valmsg-for="MainResidenceAddressSection.County" data-valmsg-replace="true"></span>    
            <div class="tooltip-container"></div>

        </div>
    </div>
</div>
        <div class="form-group">
    <div class="col-md-12">
        <label class="" for="MainResidenceAddressSection_Postcode">Postcode</label>
        <div class="row">
            <div class="controls">
                <div class="col-md-6 col-sm-8 col-xs-9">
                    <input class="tooltip form-control" data-val="true" data-val-length="Please enter a valid postcode with no more than 12 characters" data-val-length-max="12" data-val-regex="Please enter a valid postcode" data-val-regex-pattern="^(([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$)" data-val-required="Please enter a postcode" data-val-restrictedpostcode="Sorry, we do not operate in the postcode provided" data-val-restrictedpostcode-postcodelist="AB1,AB2,AB3,AB4,AB5,AB6,AB7,AB8,AB9,AB10,AB11,AB12,AB13,AB14,AB15,AB16,AB17,AB18,AB19,AB20,AB21,AB22,AB23,AB24,AB25,AB26,AB27,AB28,AB29,AB30,AB31,AB32,AB33,AB34,AB35,AB36,AB37,AB38,AB39,AB40,AB41,AB42,AB43,AB44,AB45,AB46,AB47,AB48,AB49,AB50,AB51,AB52,AB53,AB54,AB55,AB56,AB57,AB58,AB59,AB60,AB61,AB62,AB63,AB64,AB65,AB66,AB67,AB68,AB69,AB70,AB71,AB72,AB73,AB74,AB75,AB76,AB77,AB78,AB79,AB80,AB81,AB82,AB83,AB84,AB85,AB86,AB87,AB88,AB89,AB90,AB91,AB92,AB93,AB94,AB95,AB96,AB97,AB98,AB99,BT1,BT2,BT3,BT4,BT5,BT6,BT7,BT8,BT9,BT10,BT11,BT12,BT13,BT14,BT15,BT16,BT17,BT18,BT19,BT20,BT21,BT22,BT23,BT24,BT25,BT26,BT27,BT28,BT29,BT30,BT31,BT32,BT33,BT34,BT35,BT36,BT37,BT38,BT39,BT40,BT41,BT42,BT43,BT44,BT45,BT46,BT47,BT48,BT49,BT50,BT51,BT52,BT53,BT54,BT55,BT56,BT57,BT58,BT59,BT60,BT61,BT62,BT63,BT64,BT65,BT66,BT67,BT68,BT69,BT70,BT71,BT72,BT73,BT74,BT75,BT76,BT77,BT78,BT79,BT80,BT81,BT82,BT83,BT84,BT85,BT86,BT87,BT88,BT89,BT90,BT91,BT92,BT93,BT94,DD1,DD2,DD3,DD4,DD5,DD6,DD7,DD8,DD9,DD10,DD11,DG1,DG2,DG3,DG4,DG5,DG6,DG7,DG8,DG9,DG10,DG11,DG12,DG13,DG14,DG15,DG16,EH1,EH2,EH3,EH4,EH5,EH6,EH7,EH8,EH9,EH10,EH11,EH12,EH13,EH14,EH15,EH16,EH17,EH18,EH19,EH20,EH21,EH22,EH23,EH24,EH25,EH26,EH27,EH28,EH29,EH30,EH31,EH32,EH33,EH34,EH35,EH36,EH37,EH38,EH39,EH40,EH41,EH42,EH43,EH44,EH45,EH46,EH47,EH48,EH49,EH50,EH51,EH52,EH53,EH54,EH55,EH56,EH57,EH58,EH59,EH60,EH61,EH62,EH63,EH64,EH65,EH66,EH67,EH68,EH69,EH70,EH71,EH72,EH73,EH74,EH75,EH76,EH77,EH78,EH79,EH80,EH81,EH82,EH83,EH84,EH85,EH86,EH87,EH88,EH89,EH90,EH91,EH92,EH93,EH94,EH95,EH96,EH97,EH98,EH99,FK1,FK2,FK3,FK4,FK5,FK6,FK7,FK8,FK9,FK10,FK11,FK12,FK13,FK14,FK15,FK16,FK17,FK18,FK19,FK20,FK21,GY1,GY2,GY3,GY4,GY5,GY6,GY7,GY8,GY9,GY10,G1,G2,G3,G4,G5,G6,G7,G8,G9,G10,G11,G12,G13,G14,G15,G16,G17,G18,G19,G20,G21,G22,G23,G24,G25,G26,G27,G28,G29,G30,G31,G32,G33,G34,G35,G36,G37,G38,G39,G40,G41,G42,G43,G44,G45,G46,G47,G48,G49,G50,G51,G52,G53,G54,G55,G56,G57,G58,G59,G60,G61,G62,G63,G64,G65,G66,G67,G68,G69,G70,G71,G72,G73,G74,G75,G76,G77,G78,G79,G80,G81,G82,G83,G84,G85,G86,G87,G88,G89,G90,HS1,HS2,HS3,HS4,HS5,HS6,HS7,HS8,HS9,IM1,IM2,IM3,IM4,IM5,IM6,IM7,IM8,IM9,IM86,IM87,IM99,IV1,IV2,IV3,IV4,IV5,IV6,IV7,IV8,IV9,IV10,IV11,IV12,IV13,IV14,IV15,IV16,IV17,IV18,IV19,IV20,IV21,IV22,IV23,IV24,IV25,IV26,IV27,IV28,IV29,IV30,IV31,IV32,IV33,IV34,IV35,IV36,IV37,IV38,IV39,IV40,IV41,IV42,IV43,IV44,IV45,IV46,IV47,IV48,IV49,IV50,IV51,IV52,IV53,IV54,IV55,IV56,IV57,IV58,IV59,IV60,IV61,IV62,IV63,IV64,IV65,IV66,IV67,IV68,IV69,IV70,IV71,IV72,IV73,IV74,IV75,IV76,IV77,IV78,IV79,IV80,IV81,IV82,IV83,IV84,IV85,IV86,IV87,IV88,IV89,IV90,IV91,IV92,IV93,IV94,IV95,IV96,IV97,IV98,IV99,JE1,JE2,JE3,JE4,JE5,KA1,KA2,KA3,KA4,KA5,KA6,KA7,KA8,KA9,KA10,KA11,KA12,KA13,KA14,KA15,KA16,KA17,KA18,KA19,KA20,KA21,KA22,KA23,KA24,KA25,KA26,KA27,KA28,KA29,KA30,KW1,KW2,KW3,KW4,KW5,KW6,KW7,KW8,KW9,KW10,KW11,KW12,KW13,KW14,KW15,KW16,KW17,KY1,KY2,KY3,KY4,KY5,KY6,KY7,KY8,KY9,KY10,KY11,KY12,KY13,KY14,KY15,KY16,KY99,ML1,ML2,ML3,ML4,ML5,ML6,ML7,ML8,ML9,ML10,ML11,ML12,PA1,PA2,PA3,PA4,PA5,PA6,PA7,PA8,PA9,PA10,PA11,PA12,PA13,PA14,PA15,PA16,PA17,PA18,PA19,PA20,PA21,PA22,PA23,PA24,PA25,PA26,PA27,PA28,PA29,PA30,PA31,PA32,PA33,PA34,PA35,PA36,PA37,PA38,PA39,PA40,PA41,PA42,PA43,PA44,PA45,PA46,PA47,PA48,PA49,PA50,PA51,PA52,PA53,PA54,PA55,PA56,PA57,PA58,PA59,PA60,PA61,PA62,PA63,PA64,PA65,PA66,PA67,PA68,PA69,PA70,PA71,PA72,PA73,PA74,PA75,PA76,PA77,PA78,PA79,PA80,PA81,PA82,PA83,PA84,PA85,PA86,PA87,PA88,PA89,PA90,PA91,PA92,PA93,PA94,PA95,PA96,PA97,PA98,PH1,PH2,PH3,PH4,PH5,PH6,PH7,PH8,PH9,PH10,PH11,PH12,PH13,PH14,PH15,PH16,PH17,PH18,PH19,PH20,PH21,PH22,PH23,PH24,PH25,PH26,PH27,PH28,PH29,PH30,PH31,PH32,PH33,PH34,PH35,PH36,PH37,PH38,PH39,PH40,PH41,PH42,PH43,PH44,PH45,PH46,PH47,PH48,PH49,PH50,TD1,TD2,TD3,TD4,TD5,TD6,TD7,TD8,TD9,TD10,TD11,TD12,TD13,TD14,TD15,ZE1,ZE2,ZE3" id="MainResidenceAddressSection-Postcode" maxlength="12" name="MainResidenceAddressSection.Postcode" placeholder="" type="text" value="">
                </div>
                <div class="col-md-6  col-sm-4  col-xs-3">
                    <span class="valid-tick hidden-sm hidden-xs"></span>
                                    </div>
            </div>
            <span class="field-validation-valid" data-valmsg-for="MainResidenceAddressSection.Postcode" data-valmsg-replace="true"></span>  
            <div class="tooltip-container"></div>

        </div>
    </div>
</div>
        <input id="MainResidenceAddressSection_HasMainAddress" name="MainResidenceAddressSection.HasMainAddress" type="hidden" value="False">
        </div>

    </div>
</div>

好的 JQuery 通过迭代所有对其进行验证的元素来进行验证(没有元素无法调用验证),这意味着如果有人想验证更复杂的东西,比如一组元素,他将不得不 'fudge' 它通过向某些父元素添加验证并从那里开始黑客攻击。

相反,我引入了一个集合,可以向其中添加验证函数 returns bool 如果任何函数没有 return 添加 true 方法 validationMethodErrors 如果还有更多大于零错误 - 验证失败。

所以验证者得到了validationMethods

$.validator.validationMethods = [];

然后扩展为

validationMethodErrors : [],

checkForm() 必须变成这样:

    checkForm: function() {
        this.prepareForm();
        for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
            this.check( elements[i] );
        }
        for (var j = 0; j < $.validator.validationMethods.length; j++) {
            if (!$.validator.validationMethods[j]()) {
                this.validationMethodErrors.push($.validator.validationMethods[j]);
            }
        }

        return this.valid();
    },

有效()这个:

    valid: function() {
        return this.size() == 0 && this.validationMethodErrors.length == 0;
    },

然后是清理函数:

    reset: function() {
        this.successList = [];
        this.errorList = [];
        this.errorMap = {};
        this.toShow = $([]);
        this.toHide = $([]);
        this.currentElements = $([]);
        this.validationMethodErrors = [];
    },

    resetForm: function() {
        if ( $.fn.resetForm )
            $( this.currentForm ).resetForm();
        this.submitted = {};
        this.lastElement = null;
        this.prepareForm();
        this.hideErrors();
        this.elements().removeClass(this.settings.errorClass);
        this.validationMethodErrors = [];
    },

然后你只需按下 validationMethods 就可以了

    $.validator.validationMethods.push(address.onFormSubmit);

虽然不够优雅,但也够用了。

顺便说一句,恕我直言,正确的解决方案是切换到 angular 或淘汰赛,因为这种要求表明用户界面过于复杂。