0

我需要建議!我有一個asp.net mvc剃鬚刀頁面,我有一個大型表格提交銀行帳戶詳細信息和其他信息,需要驗證銀行的詳細信息,如地址,最後4 ssn,國家,州等,我認爲我可以創建一個大型('billingContainer','addressContainer','accountContainer'),其中每個div包含相關字段,然後在每個容器底部有一個「下一個」和「後退」按鈕。然後在javascript/jquery中檢查這些特定字段在每個容器中是否有效。如果說例如地址字段是有效的,那麼我將啓用「下一個」按鈕來顯示/顯示'accountContainer',然後當所有這些字段被檢查並且有效時,我可以提交。因此,對於用戶來說,只有當每個頁面上的字段全部有效時,纔有2-3個頁面被遍歷!這是最好的方法,還是應該爲每個不同的容器創建表單,並且每個表單都保存視圖模型中的數據?在asp.net mvc剃鬚刀頁面驗證jQuery中的特定表單字段

Quetion - 我如何檢查只有一個容器中的所有字段的驗證 - 比如說'addressContainer',然後我可以啓用下一個按鈕並顯示'accountContainer'表單的下一部分?然後,如果用戶從一個字段中刪除文本並使其無效,我將禁用下一個按鈕,以便他/她無法移動到下一個容器?

這裏是我的html剃刀代碼,顯示每個部分周圍有一個div容器名稱,我將顯示和隱藏

$(".showAddressContainerBtn").off('click').on('click', function() { 
 
    $("#billingContainer").hide(); 
 
    $("#addressContainer").show(); 
 
    $("#Country1").prop('disabled', true); 
 

 

 
    $(".showBillingContainerBtn").off('click').on('click', function() { 
 
    $("#billingContainer").show(); 
 
    $("#addressContainer").hide(); 
 
    }); 
 

 
    $(".showAccountContainerBtn").off('click').on('click', function() { 
 
    $("#addressContainer").hide(); 
 
    $("#accountContainer").show(); 
 

 
    $(".showAddressContainerFromAccountBtn").off('click').on('click', function() { 
 
     $("#addressContainer").show(); 
 
     $("#accountContainer").hide(); 
 

 

 
    }); 
 
    }); 
 
});
<form id="bankForm"> 
 
    <div id="billingContainer"> 
 
    <div class="row"> 
 
     <h2>Billing Country</h2> 
 
    </div> 
 
    <div class="row"> 
 
     @Html.DropDownListFor(m => m.SelectedCountryId, new SelectList(Model.CountryList, "Value", "Text"), new { @id = "Country", @class = "btn-group-lg countryList form-control selectpicker" }) 
 
    </div> 
 
    <div class="row"> 
 
     <h2>Payout methods in United States</h2> 
 
    </div> 
 
    <div class="row"> 
 
     <div class="col-sm-1"> 
 
     @Html.RadioButtonFor(m => m.TransferType, "bankaccount", new { id = "BankAccount", data_label = "" }) 
 
     </div> 
 
     <div class="col-sm-11"> 
 
     <div>Bank transfer in USD ($)</div> 
 
     <div>Get paid in 5-7 business days</div> 
 
     </div> 
 
    </div> 
 

 
    <div class="row"> 
 
     <hr /> 
 
    </div> 
 

 
    <div class="row"> 
 

 
     <div class="col-sm-12"> 
 
     <a class="btn btn-lg btn-link" href="/User/Payout" style="border: 1px solid #286090"><i class="fa fa-long-arrow-left" aria-hidden="true"></i> back</a> 
 
     <button class="btn btn-lg btn-primary pull-right showAddressContainerBtn">Next <i class="fa fa-long-arrow-right" aria-hidden="true"></i></button> 
 
     </div> 
 

 
    </div> 
 
    </div> 
 
    <div id="addressContainer" style="display: none"> 
 
    <div class="row"> 
 
     <h3>What's the address for this payout method?</h3> 
 
    </div> 
 

 
    <div class="row"> 
 
     <div class="form-group"> 
 
     <label for="AddressLine1">Street address</label> @Html.TextBoxFor(m => m.StreetAddressLine1, new { @id = "AddressLine1", @class = "form-control input-lg", placeholder = "e.g. 123 Main St." }) @Html.ValidationMessageFor(m => m.StreetAddressLine1, 
 
     "", new { @class = "text-danger" }) 
 
     </div> 
 
    </div> 
 

 
    <div class="row"> 
 
     <div class="form-group"> 
 
     <label for="AddressLine2">Apt, suite, bldg. (optional)</label> @Html.TextBoxFor(m => m.StreetAddressLine2, new { @id = "AddressLine2", @class = "form-control input-lg", placeholder = "e.g. Apt #6" }) @Html.ValidationMessageFor(m => m.StreetAddressLine2, 
 
     "", new { @class = "text-danger" }) 
 
     </div> 
 
    </div> 
 

 

 
    <div class="row"> 
 
     <div class="col-sm-6" style="padding-left: 0px; padding-right: 5px;"> 
 
     <div class="form-group"> 
 
      <label for="City">City</label> @Html.TextBoxFor(m => m.City, new { @id = "City", @class = "form-control input-lg" }) @Html.ValidationMessageFor(m => m.City, "", new { @class = "text-danger" }) 
 
     </div> 
 
     </div> 
 
     <div class="col-sm-6" style="padding-left: 5px; padding-right: 0px;"> 
 
     <div class="form-group"> 
 
      <label for="State">State/Province</label> @Html.DropDownListFor(m => m.StateCode, new SelectList(Model.StateList, "Value", "Text"), "", new { @id = "State", @class = "btn-group-lg countryList form-control selectpicker" }) @Html.ValidationMessageFor(m 
 
      => m.StateCode, "", new { @class = "text-danger" }) 
 
     </div> 
 

 
     </div> 
 
    </div> 
 

 
    <div class="row"> 
 
     <div class="form-group"> 
 
     <label for="PostalCode">Zip code/Postal code</label> @Html.TextBoxFor(m => m.PostalCode, new { @id = "PostalCode", @class = "form-control input-lg", placeholder = "e.g. 94102" }) @Html.ValidationMessageFor(m => m.PostalCode, "", new { @class 
 
     = "text-danger" }) 
 
     </div> 
 
    </div> 
 

 
    <div class="row"> 
 
     @Html.DropDownListFor(m => m.SelectedCountryId, new SelectList(Model.CountryList, "Value", "Text"), new { @id = "Country1", @class = "btn-group-lg countryList form-control selectpicker" }) 
 
    </div> 
 

 
    <div class="row"> 
 
     <hr /> 
 
    </div> 
 

 
    <div class="row"> 
 

 
     <div class="col-sm-12"> 
 
     <a class="btn btn-lg btn-link showBillingContainerBtn" style="border: 1px solid #286090"><i class="fa fa-long-arrow-left" aria-hidden="true"></i> back</a> 
 
     <button class="btn btn-lg btn-primary pull-right showAccountContainerBtn">Next <i class="fa fa-long-arrow-right" aria-hidden="true"></i></button> 
 
     </div> 
 

 
    </div> 
 

 
    </div> 
 
    <div id="accountContainer" style="display: none;"> 
 

 
    <div class="row"> 
 
     <h2>Add bank account info</h2> 
 
    </div> 
 

 
    <div class="row"> 
 
     <div class="form-group"> 
 
     <label for="LastFour">Last 4 SSN</label> @Html.TextBoxFor(m => m.LastFour, new { @id = "LastFour", @class = "form-control input-lg", placeholder = "e.g. 4530" }) @Html.ValidationMessageFor(m => m.LastFour, "", new { @class = "text-danger" }) 
 
     </div> 
 
    </div> 
 

 
    <div class="row"> 
 
     <div class="form-group"> 
 
     <label for="AccountName">Account holder name</label> @Html.TextBoxFor(m => m.AccountName, new { @id = "AccountName", @class = "form-control input-lg", placeholder = "e.g. First Last" }) @Html.ValidationMessageFor(m => m.AccountName, "", new { 
 
     @class = "text-danger" }) 
 
     </div> 
 
    </div> 
 

 
    <div class="row"> 
 
     <div class="form-group"> 
 
     <label for="Routing">Routing number</label> @Html.TextBoxFor(m => m.Routing, new { @id = "Routing", @class = "form-control input-lg", placeholder = "e.g. 123456789" }) @Html.ValidationMessageFor(m => m.Routing, "", new { @class = "text-danger" 
 
     }) 
 
     </div> 
 
    </div> 
 

 
    <div class="row"> 
 
     <div class="form-group"> 
 
     <label for="Account">Account number</label> @Html.TextBoxFor(m => m.Account, new { @id = "Account", @class = "form-control input-lg", placeholder = "e.g. 1234567890" }) @Html.ValidationMessageFor(m => m.Account, "", new { @class = "text-danger" 
 
     }) 
 
     </div> 
 
    </div> 
 

 
    <div class="row"> 
 
     <div class="form-group"> 
 
     <label for="ConfirmAccount">Confirm account number</label> @Html.TextBoxFor(m => m.ConfirmAccount, new { @id = "ConfirmAccount", @class = "form-control input-lg", placeholder = "e.g. 1234567890" }) @Html.ValidationMessageFor(m => m.ConfirmAccount, 
 
     "", new { @class = "text-danger" }) 
 
     </div> 
 
    </div> 
 

 
    <div class="row"> 
 

 
     <div class="col-sm-12"> 
 
     <a class="btn btn-lg btn-link showAddressContainerFromAccountBtn" style="border: 1px solid #286090"><i class="fa fa-long-arrow-left" aria-hidden="true"></i> back</a> 
 
     <button class="btn btn-lg btn-primary pull-right addAccountBtn">Next <i class="fa fa-long-arrow-right" aria-hidden="true"></i></button> 
 
     </div> 
 

 
    </div> 
 

 
    </div> 
 
</form>

+1

驗證控件組,請參考[本答案](https://stackoverflow.com/questions/25643394/mvc-force-jquery-validation-on-group-of-elements/25645097#25645097) –

回答

0

我怎麼能檢查確認爲只有一個容器中的所有字段 - 比如'addressContainer',所以我可以啓用下一個按鈕並顯示'accountContainer'表單的下一部分?

解決方案:我建議您逐步對每個字段執行內聯驗證,然後如果用戶驗證所有字段,則允許他執行某些操作或將其發回服務器。有關詳細信息,請查看該鏈接Click here for inline validation plugin

also you can get some help from here

然後,如果用戶從一個字段刪除文字和無效的話,我會禁用下一個按鈕,這樣他/她不能移動到下一個容器?

解決方案:您可以使用onTextChange函數那樣的東西,作爲整體,您的案例中的完整解決方案將進行內聯驗證。