如何在数据验证触发时显示验证div

时间:2013-05-15 06:49:54

标签: asp.net-mvc html5 twitter-bootstrap validation

我不想使用自定义方式设置Html.ValidationSummary()生成的data-valmsg-summary样式,而是只要在字段验证失败时显示应用了twitter引导程序样式的框。我该怎么做呢?目前我的标记看起来像这样:

..
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>

<div class="container">
    <div class="row-fluid">
        <br />
        <br />
        <br />
        <br />
    </div>
    <div class="row-fluid">
        <form class="navbar-form pull-right" action="/Login?ReturnUrl=%2F" method="post">
            <h3 class="modal-header">Please sign in</h3>
            <input data-val="true" data-val-required="The&#32;Username&#32;field&#32;is&#32;required." id="Username" name="Username" type="text" class="input-large" placeholder="Username">
            <input data-val="true" data-val-required="The&#32;Password&#32;field&#32;is&#32;required." id="Password" name="Password" type="password" class="input-large" placeholder="Password">
            <label class="checkbox">
                <input type="checkbox" value="remember-me">
                Remember me
            </label>
            <button type="submit" class="btn btn-danger">Sign in</button>
            <br />
            <br/>
            <div data-valmsg-summary="true" class="alert alert-danger alert-block" id="formval" >
                <span class="close pull-right" data-dismiss="alert">&times;</span>
                <strong>Ooops!</strong> You seem to be missing something:
                <ul>
                    <li style="display: none"></li>
                </ul>
            </div>
        </form>
    </div>
</div>

我已经尝试将style =“display:none”添加到我的div中,但这似乎也无法解决问题。

1 个答案:

答案 0 :(得分:1)

我正在寻找别的东西,偶然发现了这一点。以为我会为下一个人发布答案,因为我迟到了5个月。准备好添加到您的文档。

//I dont want to validate my ajax forms take that if statement out if you want 2.
if ($('form:not([data-ajax="true"])').length != 0) {
        var settings = $.data($('form:not([data-ajax="true"])')[0], 'validator').settings;
        settings.submitHandler = function (form) {
            //success
            form.submit();
        };
    }


$("form").bind("invalid-form.validate", function (form, validator) {

    var errors = validator.numberOfInvalids();
    var message = "<ul>";

     //loop thru the errors
    for (var x = 0; x < validator.errorList.length; x++) {
        var $group = $(validator.errorList[x].element).parent().parent(); //gets bootstrap class of form-group
        var $element = $(validator.errorList[x].element); // gets the element to validate
        var elementMessage = validator.errorList[x].message; // gets the message
        $group.addClass("has-error"); // adds the bootstrap class has-error to the group
        $element.popover({ content: elementMessage, placement: "right" }).popover("show"); // adds a popover

        message += "<li>" + elementMessage + "</li>"; //appends message to list
    }
    message += "</ul>";

    // Function I have to add alert to the page, but basically you can do whatever you want with the message now.
    RegisterError("There was some errors with your submission!", message, false); 
});