【问题标题】:How to display validation error summary at top in MVC5 with bootstrap?如何使用引导程序在 MVC5 顶部显示验证错误摘要?
【发布时间】:2017-12-05 04:45:38
【问题描述】:

我试图在顶部显示验证错误摘要并希望提供一些样式,但不幸的是,当我尝试单击表单提交按钮时,它会显示错误但每个控件而不是顶部。

我想装饰内置验证摘要,而不是创建自己的 JavaScript 验证函数。

我想做的是,请看我的代码。

        $(document).ready(function () {

        });

        function submitForm() {
            if ($("#frmR").valid()) {
                var form = $("#frmR").serialize();
                $.post("/Portal/Registration/RegisterP", form, function (msg) {

                });
            }
      }

    function Shift() {
        window.history.back();
        return false;
    }
input[type="checkbox"], input[type="radio"] {
    padding: 0;
    box-sizing: border-box;
    width: 30px;
    height: 30px;
    background-color: #edf8f7;
    /* background-image: none; */
    border: 1px solid #8dc6cd;
    border-radius: 0px;
}

input[type=checkbox], input[type=radio] {
    margin: 4px 0 0;
    margin-top: 1px\9;
    line-height: normal;
}

input[type=checkbox], input[type=radio] {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    padding: 0;
}

button, input, select, textarea {
    font-family: inherit;
    font-size: inherit;
    line-height: inherit;
}

input {
    line-height: normal;
}

button, input, optgroup, select, textarea {
    margin: 0;
    font: inherit;
    color: inherit;
}

* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

input[type="radio" i] {
    margin: 3px 3px 0px 5px;
}

input[type="radio" i] {
    -webkit-appearance: radio;
    box-sizing: border-box;
}

input[type="radio" i], input[type="checkbox" i] {
    background-color: initial;
    margin: 3px 0.5ex;
    padding: initial;
    border: initial;
}

input {
    -webkit-appearance: textfield;
    background-color: white;
    -webkit-rtl-ordering: logical;
    user-select: text;
    cursor: auto;
    padding: 1px;
    border-width: 2px;
    border-style: inset;
    border-color: initial;
    border-image: initial;
}

input, textarea, select, button {
    text-rendering: auto;
    color: initial;
    letter-spacing: normal;
    word-spacing: normal;
    text-transform: none;
    text-indent: 0px;
    text-shadow: none;
    display: inline-block;
    text-align: start;
    margin: 0em;
    font: 13.3333px Arial;
}

input, textarea, select, button, meter, progress {
    -webkit-writing-mode: horizontal-tb;
}
label {
    display: inline-block;
    margin-bottom: 5px;
    /* font-weight: bold; */
    color: #6b7b8a;
    font-size: 18px;
    font-weight: bold;
}

label {
    display: inline-block;
    max-width: 100%;
    margin-bottom: 5px;
    font-weight: 700;
}
body {
    background: #fff;
    font-family: 'Roboto', sans-serif;
    color: #54667a;
    line-height: 22px;
}

body {
    font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
    font-size: 14px;
    line-height: 1.42857143;
    color: #333;
    background-color: #fff;
}
label>input[type="radio"]{
  vertical-align: -30%;
}
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script 
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" > </script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js" > </script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.6/jquery.validate.unobtrusive.min.js" > </script>
</head>
<body>
<form  id="frmR" action="#" method="post">    
                     <div class="validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li>
</ul></div>
                     <div class="col-xs-6 remove-padding">
                         <h4>Patient's Appointment Type</h4>
                     </div>
                     <div class="col-xs-6">
                         <button type="submit" id="register-submit-btn" onclick="submitForm(); return false;" class="btn-primarySmall pull-right">
                             Register
                         </button>
                    </div>
                         <div class="row">
                             <div class="col-xs-12">
                                 <div class="col-xs-12">
                                     <div>
                                         <label class="">Type</label>
                                         <label><input type="radio" name="Type" checked="" value="Walk-In">Walk-In</label>
                                         <label><input type="radio" name="Type" value="Telephonic">Telephonic</label>
                                     </div>
                                 </div>
                                 <div class="col-xs-12 remove-padding">
                                     <hr />
                                     <h4>Patient's Personal Information</h4>
                                     <hr />
                                 </div>

                                 <div class="col-xs-4">
                                     <div class="form-group">
                                         <label>First Name<span style="color:#FF0000">*</span></label>
                                         <input class="form-control" type="text" placeholder="First Name" name="vFirstName" id="vFirstName" required="required" />
                                     </div>
                                 </div>
                            </div>
                        </div>
</form>
</body>
</html>

【问题讨论】:

  • 请不要编辑以将解决方案中的代码合并到您的问题中。这是没有意义的,如果正确的代码已经内置到问题中,那么下面的答案就不再有意义了。把它卷了回来。您也不会将.validate() 放在submit 函数中...它用于在页面加载时初始化插件,并且不会在每次单击按钮时被调用。

标签: jquery asp.net-mvc jquery-validate


【解决方案1】:

您可以尝试errorPlacement jQuery Validation 的功能来装饰并在顶部显示所有错误:

您需要在表单上方添加波纹管 html

<ul class="errorList"></ul>

jQuery:

$("#frmR").validate({
    errorPlacement: function(error, element) {
        element = element.closest('li');
        element = element.children(':first-child');
        error.insertBefore(element);
        error.addClass('message');
        $(function() {                                 // my function
            var errorIndex = $(error).index('div');
            var errorId = 'errordiv' + errorIndex.toString();
            $(error).attr('id', errorId);
            $('.errorList').append('<li><a href="#' + errorId + '">' + $(error).text() + '</a></li>');
        });    
    }
});

【讨论】:

  • 谢谢Bharatsing。我是否以正确的方式做事。我在准备好的事件中使用了这些代码行。 var 验证器 = $('form').data('validator'); validator.settings.errorPlacement = function (error, element) { // 你的代码行 }); };但它抛出一个错误。未捕获的类型错误:无法读取未定义的属性“设置”
  • 我已经编辑了我的答案。您需要在 $("#frmR").validate(); 中添加功能功能
  • 谢谢。现在看到上面的帖子,它正在工作。谢谢
  • 多么迂回的方式。 @user6306245,您可以简单地使用 errorLabelContainer 选项自动处理所有这些。 jqueryvalidation.org/validate/#errorlabelcontainer
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
  • 2018-01-11
  • 1970-01-01
相关资源
最近更新 更多