表单无效时,字段验证错误地启用

时间:2015-07-24 18:23:44

标签: twitter-bootstrap formvalidation-plugin

下图显示了具有正确验证字段的注册表单。 其他字段尚未完成,但代码以某种方式过早地启用了响应click事件的提交按钮。我正在使用formvalidation.io

valid field form invalid

我想知道是否有人可以在下面粘贴的示例代码中看到问题所在。 非常感谢 凯文

 <script src="/formval/dist/js/formValidation.min.js"></script>
 <script src="/formval/dist/js/framework/bootstrap.min.js"></script>
 <div class="container">
 <div class="row">
    <div class="col-sm-6 col-md-4 col-md-offset-4">
        <div class="account-wall">
            <img class="profile-img" src="images/cl.png"
                alt="">
            <form id="regid" class="form-group">
            <div class = "form-group" > <input type="text" class="form-control" placeholder="Company name" id="cn" > </div>
            <div class = "form-group"> <input type="text" class="form-control" placeholder="User name" id="un" name="un" > </div>
            <div class = "form-group"> <input type="email" class="form-control" placeholder="Email" id="em" name="em" > </div>
            <div class = "form-group"> <input type="password" class="form-control" placeholder="Password" id="pw" name="pw" > </div>
            <div class = "form-group"> <input type="password" class="form-control" placeholder="Password repeat" id="pwr" name="pwr" > </div>
            <div class = "form-group">
            <button class="btn btn-lg btn-primary btn-block btn-primary" type="submit" id="submit" disabled >Register</button>
            </div>
            </form>
            </div>
    </div>
</div>
</div>
<script>
$(document).ready(function() {


$('#regid').formValidation({
   framework:'bootstrap',
   icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            cn: {    },
            un: {
                validators: {
                    notEmpty: {
                        message: 'The username is required'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: 'The username must be more than 6 and less than 30 characters long'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: 'The username can only consist of alphabetical, number, dot and underscore'
                    },
                    blank: {}
                }
            },
            em: {
                validators: {
                    notEmpty: {
                        message: 'The email address is required'
                    },
                                                                                                                                          },
            emailAddress: {
                        message: 'The input is not a valid email address'
                    },
                    blank: {}
                }
            },
            pw: {
                validators: {
                    notEmpty: {
                        message: 'The password is required'
                    },
                    stringLength: {
                        min: 8,
                        max: 30,
                        message: 'The password must be between 8 and 30 characters long'
                    },

                    //blank: {}
                }

            },
            pwr: {
                validators: {
                    notEmpty: {
                        message: 'The repeat password is required'
                    },
                    stringLength: {
                        min: 8,
                        max: 30,
                        message: 'The password must be between 8 and 30 characters long'
                    },
                    //blank: {}
                }
            } 
       }
     }).on('success.form.fv', function(e) {
        e.preventDefault();
        console.log('submit');            
      });

1 个答案:

答案 0 :(得分:2)

我认为您要禁用提交按钮,直到所有字段都有效,然后启用它。

要执行此操作,请触发success.field.fv事件并检查是否至少有一个字段尚未经过验证或正在验证,并使用disableSubmitButtons方法禁用提交按钮,请参阅以下内容代码:

$(document).ready(function() {
    $('#regid').formValidation({
        // ...
    })
    // <===
    .on('success.field.fv', function (e, data) {
        // Check if there is at least one field which is not validated yet
        // or being validated
        if (data.fv.isValid() === null) {
            data.fv.disableSubmitButtons(true);
        }
    })
    // ===>
    .on('success.form.fv', function(e) {
        e.preventDefault();
        console.log('submit');            
    });
});

#Working exmaple: http://jsfiddle.net/Arkni/f0m15rqq/

#参考文献:

`