我想驗證我的表單,因爲如果它已經存在列表中,我不能在代碼中輸入代碼。使用jQuery驗證驗證自定義方法
<input id="form:inptCode" type="text" name="form:inptCode" class="number">
Bellow是primefaces orderlist元素的html。
<select id="form:choices_values" name="form:choices_values" multiple="true" class="ui-helper-hidden">
<option value="1" selected="selected">1</option>
</select>
因此,該列表包含值1.如果我在inputText中鍵入數字1,應該出現錯誤消息。
爲了達到這個目的,我添加了一個方法,並且當這個方法返回false時我嘗試驗證窗體。 我沒有太多使用jQuery驗證的經驗,實際上我只寫了幾個方法。
$.validator.addMethod('codExistss', function (value, element) {
var lstChoices = document.getElementById('form:choices_values');
for (i = 0; i < lstChoices.options.length; i++) {
return value === lstChoices.option[i].value;
}
}, "The code already exists.");
$(document).ready(function() {
$('#form').validate({
rules: {
'form:inptCode': {
codExistss: false
}
}
});
});
看來我的代碼不起作用。那麼,你能幫我理解爲什麼嗎?
更改 「<」 來<。 –
我剛剛在這裏寫了錯誤的名稱,但在我的頁面中它是正確的,並且該方法不起作用。 –
你有任何控制檯錯誤? –