【问题标题】:jqBootstrap Validation Form ErrorjqBootstrap 验证表单错误
【发布时间】:2013-08-13 10:50:27
【问题描述】:

我正在尝试验证表单,但问题是代码没有按照要求验证表单,下面是代码

<script>
$(function() {
    $("input,textarea,select").jqBootstrapValidation(
        {
            preventSubmit: true,
            submitError: function($form, event, errors) {
                // Here I do nothing, but you could do something like display 
                // the error messages to the user, log, etc.
            },
            submitSuccess: function($form, event) {
                alert("OK");
                event.preventDefault();
            },
            filter: function() {
                return $(this).is(":visible");
            }
        }
   );
});
</script>

在所有情况下,无论表格是空的还是填充的,它都会成功。

我搜索了很多,但无法找出问题所在。

【问题讨论】:

    标签: javascript html validation twitter-bootstrap


    【解决方案1】:

    首先你必须包含所有必需的 css 和 js 文件

    http://reactiveraven.github.io/jqBootstrapValidation/js/jqBootstrapValidation.js
    http://reactiveraven.github.io/jqBootstrapValidation/css/bootstrap.css
    http://reactiveraven.github.io/jqBootstrapValidation/js/jQuery-1.7.2-min.js
    

    html:

    <form class="form-horizontal" novalidate>
        <div class="control-group">
            <label class="control-label" for="email">Email address</label>
            <div class="controls">
                <input type="email" name="email" id="email" required>
                <p class="help-block">Email address we can contact you on</p>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="emailAgain">Email again</label>
            <div class="controls">
                <input type="email" data-validation-matches-match="email" data-validation-matches-message="Must match email address entered above" id="emailAgain" name="emailAgain">
                <p class="help-block">And again, to check for speeling miskates</p>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="terms-and-conditions">Legal</label>
            <div class="controls">
                <label class="checkbox">
                    <input type="checkbox" id="terms-and-conditions" name="terms-and-conditions" required data-validation-required-message="You must agree to the terms and conditions">I agree to the <a href="#">terms and conditions</a>
    
                </label>
                <p class="help-block"></p>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">Quality Control</label>
            <div class="controls">
                <label class="checkbox">
                    <input type="checkbox" name="qualityControl[]" value="fast" data-validation-minchecked-minchecked="2" data-validation-minchecked-message="Choose two" data-validation-maxchecked-maxchecked="2" data-validation-maxchecked-message="You can't have it all ways">Fast</label>
                <label class="checkbox">
                    <input type="checkbox" name="qualityControl[]" value="cheap">Cheap</label>
                <label class="checkbox">
                    <input type="checkbox" name="qualityControl[]" value="good">Good</label>
                <p class="help-block"></p>
            </div>
        </div>
        <div class="form-actions">
            <button type="submit" class="btn btn-primary">Test Validation <i class="icon-ok icon-white"></i>
            </button>
            <br />(go ahead, nothing is sent anywhere)</div>
    </form>
    

    脚本

    <script>
            $(function() {
                $("input,textarea,select").jqBootstrapValidation(
                    {
                        preventSubmit: true,
                        submitError: function($form, event, errors) {
                            // Here I do nothing, but you could do something like display 
                            // the error messages to the user, log, etc.
                        },
                        submitSuccess: function($form, event) {
                            alert("OK");
                            event.preventDefault();
                        },
                        filter: function() {
                            return $(this).is(":visible");
                        }
                    }
                );
            });
     </script>
    

    jsfiddle 演示

    http://jsfiddle.net/suhailvs0/Dp3qZ/1/

    【讨论】:

    • 问题是我在代码之前包含的引导文件版本。最新版本1.3.6有错误谢谢..
    • 我遇到了 bootstrap.css 版本 3.x 的问题。当我包含我的 css 文件时,数据验证消息不起作用。
    猜你喜欢
    • 2013-01-09
    • 1970-01-01
    • 2012-07-01
    • 2011-09-18
    • 2018-12-05
    • 2016-03-20
    • 2011-02-21
    • 2023-03-11
    相关资源
    最近更新 更多