【问题标题】:How to call a validation callback function within formValidation plugin?如何在 formValidation 插件中调用验证回调函数?
【发布时间】:2016-06-21 13:41:07
【问题描述】:

我在 HTML5 表单中使用来自 http://formvalidation.io/examples/ 的 formValidation 插件对表单输入执行验证。

notEmpty 等标准检查在输入上按预期工作。但是现在我遇到了一个需要根据列表验证输入的情况。我已经编写了函数 checkEMIDExists() 来执行此操作,但还没有弄清楚如何从 formValidation 插件中调用它。

This is the example I've followed 为了尝试实现回调函数。但是在运行时,当填写EM 输入值时,回调函数不会触发。

我在回调中设置了一个警报, 每次我更改输入值时都会触发。我还通过在change 事件上触发它来验证checkEMIDExists() 的工作原理,它确实有效。

看来我返回布尔验证结果的方式不正确。

问题:

如何在 formValidation 插件中调用回调函数?

代码:(要点)

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>

【问题讨论】:

    标签: javascript jquery validation twitter-bootstrap-3 callback


    【解决方案1】:

    如果验证失败,您的回调方法也应该返回 false。 null 返回值被忽略。

    将您的回调返回语句更改为:

    return isEMIDMatch;
    

    或者更简洁,尽管可读性较差:

    return checkEMIDExists(emidVal);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多