1
我使用HTML5格式的http://formvalidation.io/examples/的formValidation插件對錶單輸入執行驗證。如何在formValidation插件中調用驗證回調函數?
標準檢查如notEmpty
按預期在輸入上工作。但是現在我有一個情況,輸入需要根據列表進行驗證。我已經編寫了函數checkEMIDExists()
來做到這一點,但還沒有想出如何從formValidation插件中調用它。
This is the example I've followed爲了試試器的回調函數。但是在運行時,填寫EM
輸入值時回調函數不會觸發。
我已經在回調中設置了一個警報,確實是每當我改變輸入值時觸發。我還驗證了checkEMIDExists()
通過在change
事件上觸發它的工作,它確實有效。
看來,我返回bool驗證結果的方式不正確。
問:
我怎麼能說formValidation插件中的回調函數?
代碼:(GIST)
EM輸入元素 -
<input id="EscID" name="EM" maxlength="10" type="text" data-error="EM already exists or none supplied" placeholder="(If Applicable)" class="form-control">
腳本 -
<script>
//List EM input is validated against
var escHistoryList = @Html.Raw(Json.Encode(Model.EscHistory));
$(document).ready(function() {
var $createForm = $('#createForm');
//Validate the required input fields to prevent submit if not
//valid input.
$('#createForm').formValidation({
framework: 'bootstrap',
icon: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields:
Application: {
validators: {
notEmpty: {
message: 'The Application name field is required'
}
}
},
EM: {
validators: {
callback: {
message: 'A record with this EPRID already exists!',
callback: function (value, validator, $field) {
// Determine if the input EPRID already exists
var emidVal = $('#EscalationID').val();
alert("IN VALIDATOR CALLBACK");
var isEMIDMatch = false;
isEMIDMatch = checkEMIDExists(emidVal);
if(isEMIDMatch)
return true;
}
}
}
}
}
});
//Determineif input EMID exists in list
function checkEMIDExists(emidVal){
var isMatch = false;
for(var i=0;i<escHistoryList.length;i++){
if(escHistoryList[i]["EM"].indexOf(emidVal) > -1){
isMatch = true;
break;
}
}
return isMatch;
}
});//end $(document).ready
</script>