Javascript 如何在formValidation插件中调用验证回调函数?
我使用HTML5表单中的formValidation插件对表单输入执行验证 标准检查,如Javascript 如何在formValidation插件中调用验证回调函数?,javascript,jquery,validation,twitter-bootstrap-3,callback,Javascript,Jquery,Validation,Twitter Bootstrap 3,Callback,我使用HTML5表单中的formValidation插件对表单输入执行验证 标准检查,如notEmpty在输入端按预期工作。但是现在我有一个案例,输入需要根据列表进行验证。我已经编写了函数checkEMIDExists()来实现这一点,但还没有弄清楚如何从formValidation插件中调用它 为了尝试实现回调函数。但在运行时,在填写EM输入值时,回调函数不会启动 我在回调中设置了一个警报,每次更改输入值时都会触发该警报。我还验证了checkEMIDExists()通过在change事件中触发
notEmpty
在输入端按预期工作。但是现在我有一个案例,输入需要根据列表进行验证。我已经编写了函数checkEMIDExists()
来实现这一点,但还没有弄清楚如何从formValidation插件中调用它
为了尝试实现回调函数。但在运行时,在填写EM
输入值时,回调函数不会启动
我在回调中设置了一个警报,每次更改输入值时都会触发该警报。我还验证了checkEMIDExists()
通过在change
事件中触发它来工作,并且它确实工作
我返回布尔验证结果的方式似乎不正确
问题:
如何在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>
脚本-
<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>
//列表EM输入根据验证
var escHistoryList=@Html.Raw(Json.Encode(Model.eschhistory));
$(文档).ready(函数(){
var$createForm=$(“#createForm”);
//验证所需的输入字段以防止提交(如果未提交)
//有效输入。
$('#createForm')。formValidation({
框架:“bootstrap”,
图标:{
有效:“glyphicon glyphicon ok”,
无效:“glyphicon glyphicon删除”,
正在验证:“glyphicon glyphicon刷新”
},
领域:
应用程序:{
验证器:{
注意:{
消息:“应用程序名称字段是必需的”
}
}
},
EM:{
验证器:{
回拨:{
消息:“具有此EPRID的记录已存在!”,
回调:函数(值,验证器,$field){
//确定输入EPRID是否已经存在
var emidVal=$('#EscalationID').val();
警报(“在验证程序回调中”);
var isEMIDMatch=false;
isEMIDMatch=检查EMIDexists(emidVal);
如果(isEMIDMatch)
返回true;
}
}
}
}
}
});
//确定列表中是否存在输入EMID
功能检查emidexists(emidVal){
var isMatch=错误;
对于(变量i=0;i-1){
isMatch=true;
打破
}
}
返回isMatch;
}
});//结束$(文档)。准备好了吗
如果验证失败,回调方法也应该返回false。
将忽略空返回值
将回调返回语句更改为:
return isEMIDMatch;
或者更简洁但可读性较差:
return checkEMIDExists(emidVal);