我在HTML5表单中使用http://formvalidation.io/examples/中的formValidation插件对表单输入执行验证。
notEmpty
等标准检查在输入上按预期工作。但现在我有一个案例,输入需要根据列表进行验证。我已经编写了函数checkEMIDExists()
来执行此操作但尚未找到如何从formValidation插件中调用它。
This is the example I've followed为了尝试实现回调函数。但是在运行期间,当填写EM
输入值时,回调函数不会触发。
我在每次更改输入值时 触发的回调中设置了警报。我还通过在checkEMIDExists()
事件上触发change
来验证 <input id="EscID" name="EM" maxlength="10" type="text" data-error="EM already exists or none supplied" placeholder="(If Applicable)" class="form-control">
是否正常工作,并确实有效。
似乎我返回bool验证结果的方式并不正确。
问题:
如何在formValidation插件中调用回调函数?
代码:(要点)
EM输入元素 -
<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>
脚本 -
#' Cut2 from Hmisc
#'
#' Shamelessly imported from Hmisc, which I don't like to load because of name collisions.
#' @seealso \link[Hmisc]{cut2}
#' @export
cut2 <- Hmisc::cut2
答案 0 :(得分:1)
如果验证失败,您的回调方法也应该返回false。 空返回值将被忽略。
将您的回调return语句更改为:
return isEMIDMatch;
或者甚至可能更简洁,但可读性更低:
return checkEMIDExists(emidVal);