我們正在使用 FormValidation.io 進行表單驗證,現在我們正在嘗試使用相同的驗證器,使用通過宣告性插件應用的 html 屬性。
我們想要什么?我們希望驗證檢查確認欄位是否與密碼欄位相同,否則驗證失敗
<input class="form-control" data-fv-not-empty="true" data-fv-not-empty___message="The password is required" type="password" placeholder="Password" name="password" id="input-password" autocomplete="off">
<input class="form-control" data-fv-identical="true" data-fv-identical___compare="getPassword()" data-fv-identical___message="The password and its confirm are not the same" type="password" placeholder="Confirm Password" name="confirm" id="input-confirm" autocomplete="off">
<script>
function getPassword(){
return $('#input-password').val();
}
</scrpt>
現在檔案宣告我們可以將比較值宣告為字串或回傳字串的函式。現在,即使我們在那里呼叫一個函式,它仍然會將值轉換為字串,因此根據我們當前的代碼,confirm 將顯示為無效,直到 confirm 的值不等于"getPassword()"該函式的輸出,而是這個確切的字串。
我們想知道如何使用 html 屬性設定比較。我們可以使用編程方式實作相同的功能,但我們希望使用宣告模式使其作業
檔案鏈接
因此,如果不修改核心插件本身,我們將無法實作這一點。
希望這可以幫助任何正在尋找相同答案的人。
uj5u.com熱心網友回復:
我只是遇到了這個問題。作為一種解決方法,我使用這個技巧:
var fv = form.fv; fv.updateValidatorOption(input.name, 'identical', 'compare', function(){ return inputConfirm.value; });當然,在我為回圈中的所有表單初始化驗證的入口點中,我將實體保存在表單道具中:
form.fv = fv;轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/340452.html

