JQuery 表单验证

huangapple go评论75阅读模式
英文:

JQuery Validation of Form

问题

我正在尝试使用Jquery验证修改errorClass和validClass,但遇到了很多麻烦。

我假设我只需要添加.validate函数并设置参数即可。

尽管验证确实触发并显示了验证消息,但类仍然被更改为标准的“input-validation-error”或“valid”。

在浏览器中没有抛出任何JQuery错误。

我认为问题可能与不显眼的插件有关,它阻止了jquery.validate设置的应用。

> JQuery  v3.7.0, Jquery-validate v1.19.5, Jquery-validate-unobtrusive
> v4.0.0

脚本:

@section Scripts {
  $(document).ready(function (e) {
            $("#myForm").validate({
                errorClass: "is-invalid",
                validClass: "is-valid"
            });
  });
}

Razor页面:

<form id="myForm" method="post">
    <div class="row">
        <div class="col-sm-3">
            <label asp-for="ADR.AircraftID" class="form-label"></label>
            <select asp-for="ADR.AircraftID" asp-items="@(new SelectList(Model.Aircraft,"AircraftID","Registration"))" class="form-select" required>
                <option></option>
            </select>
            <span asp-validation-for="ADR.AircraftID" class="text-danger"></span>
        </div>
        <div class="col-sm-3">
            <label asp-for="ADR.PersonID" class="form-label"></label>
            <select asp-for="ADR.PersonID" asp-items="@(new SelectList(Model.Person,"PersonID","FullName"))" class="form-select" required>
                <option></option>
            </select>
            <span asp-validation-for="ADR.PersonID" class="text-danger"></span>
        </div>
        <div class="col-sm-3">
            <label asp-for="ADR.Date" class="form-label"></label>
            <kendo-datepicker for="ADR.Date" class="form-control" required></kendo-datepicker>
            <span asp-validation-for="ADR.Date" class="text-danger"></span>
        </div>
        <div class="col-sm-3">
            <label asp-for="ADR.TTIS" class="form-label"></label>
            <input asp-for="ADR.TTIS" class="form-control" required />
            <span asp-validation-for="ADR.TTIS" class="text-danger"></span>
        </div>
        <div class="col-sm-12">
            <label asp-for="ADR.Details" class="form-label"></label>
            <textarea asp-for="ADR.Details" class="form-control" rows="3" required></textarea>
            <span asp-validation-for="ADR.Details" class="text-danger"></span>
        </div>
    </div>
    <br />
    <div class="form-check">
        <input asp-for="ADR.Deferred" class="form-check-input isdeferred" type="checkbox" />
        <label asp-for="ADR.Deferred" class="form-check-label"></label>
        <span asp-validation-for="ADR.Deferred" class="text-danger"></span>
    </div>
    <div class="row">
        <div class="col-sm-6">
            <label asp-for="ADR.MELRef" class="form-label"></label>
            <input asp-for="ADR.MELRef" class="form-control deferred" />
            <span asp-validation-for="ADR.MELRef" class="text-danger"></span>
        </div>
        <div class="col-sm-2">
            <label asp-for="ADR.DeferralExpiry" class="form-label"></label>
            <kendo-datepicker for="ADR.DeferralExpiry" class="form-control deferred"></kendo-datepicker>
            <span asp-validation-for="ADR.DeferralExpiry" class="text-danger"></span>
        </div>
    </div>

    <div class="d-grid gap-2 d-md-flex justify-content-md-end">
        <a class="btn btn-outline-secondary" asp-page="./Index"> Close</a>
        <input type="submit" id="Submit" value="Submit" class="btn btn-primary" />
    </div>
</form>

头部:

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - FlightLog</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" /> 

    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>

    <script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>    

    <link href="https://kendo.cdn.telerik.com/2022.2.621/styles/kendo.common-bootstrap.min.css" rel="stylesheet" type="text/css" />
    <link href="https://kendo.cdn.telerik.com/2022.2.621/styles/kendo.bootstrap.min.css" rel="stylesheet" type="text/css" /> 

    <script src="https://kendo.cdn.telerik.com/2022.2.621/js/kendo.all.min.js"></script>
    <script src="https://kendo.cdn.telerik.com/2022.2.621/js/kendo.aspnetmvc.min.js"></script>

    <script src="https://kendo.cdn.telerik.com/2022.2.510/js/cultures/kendo.culture.en-NZ.min.js"></script>       
</head>
英文:

I am having a lot of trouble trying to modify the errorClass and validClass with Jquery validation.

I assume all I need to do is add the .validate function and set the parameters.

Whilst the validation does fire and the validation message is shown, the classes are still changed to the standard "input-validation-error" or "valid".

There are no JQuery errors thrown in the browser.

I believe the problem may be something to do with the unobtrusive plugin preventing the jquery.validate settings being applied.

> JQuery v3.7.0, Jquery-validate v1.19.5, Jquery-validate-unobtrusive
> v4.0.0

Script:

@section Scripts {
$(document).ready(function (e) {
$(&quot;#myForm&quot;).validate({
errorClass: &quot;is-invalid&quot;,
validClass: &quot;is-valid&quot;
});
});
}

Razor Page:

&lt;form id=&quot;myForm&quot; method=&quot;post&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-sm-3&quot;&gt;
&lt;label asp-for=&quot;ADR.AircraftID&quot; class=&quot;form-label&quot;&gt;&lt;/label&gt;
&lt;select asp-for=&quot;ADR.AircraftID&quot; asp-items=&quot;@(new SelectList(Model.Aircraft,&quot;AircraftID&quot;,&quot;Registration&quot;))&quot; class=&quot;form-select&quot; required&gt;
&lt;option&gt;&lt;/option&gt;
&lt;/select&gt;
&lt;span asp-validation-for=&quot;ADR.AircraftID&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;col-sm-3&quot;&gt;
&lt;label asp-for=&quot;ADR.PersonID&quot; class=&quot;form-label&quot;&gt;&lt;/label&gt;
&lt;select asp-for=&quot;ADR.PersonID&quot; asp-items=&quot;@(new SelectList(Model.Person,&quot;PersonID&quot;,&quot;FullName&quot;))&quot; class=&quot;form-select&quot; required&gt;
&lt;option&gt;&lt;/option&gt;
&lt;/select&gt;
&lt;span asp-validation-for=&quot;ADR.PersonID&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;col-sm-3&quot;&gt;
&lt;label asp-for=&quot;ADR.Date&quot; class=&quot;form-label&quot;&gt;&lt;/label&gt;
&lt;kendo-datepicker for=&quot;ADR.Date&quot; class=&quot;form-control&quot; required&gt;&lt;/kendo-datepicker&gt;
&lt;span asp-validation-for=&quot;ADR.Date&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;col-sm-3&quot;&gt;
&lt;label asp-for=&quot;ADR.TTIS&quot; class=&quot;form-label&quot;&gt;&lt;/label&gt;
&lt;input asp-for=&quot;ADR.TTIS&quot; class=&quot;form-control&quot; required /&gt;
&lt;span asp-validation-for=&quot;ADR.TTIS&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;col-sm-12&quot;&gt;
&lt;label asp-for=&quot;ADR.Details&quot; class=&quot;form-label&quot;&gt;&lt;/label&gt;
&lt;textarea asp-for=&quot;ADR.Details&quot; class=&quot;form-control&quot; rows=&quot;3&quot; required&gt;&lt;/textarea&gt;
&lt;span asp-validation-for=&quot;ADR.Details&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input asp-for=&quot;ADR.Deferred&quot; class=&quot;form-check-input isdeferred&quot; type=&quot;checkbox&quot; /&gt;
&lt;label asp-for=&quot;ADR.Deferred&quot; class=&quot;form-check-label&quot;&gt;&lt;/label&gt;
&lt;span asp-validation-for=&quot;ADR.Deferred&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-sm-6&quot;&gt;
&lt;label asp-for=&quot;ADR.MELRef&quot; class=&quot;form-label&quot;&gt;&lt;/label&gt;
&lt;input asp-for=&quot;ADR.MELRef&quot; class=&quot;form-control deferred&quot; /&gt;
&lt;span asp-validation-for=&quot;ADR.MELRef&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;div class=&quot;col-sm-2&quot;&gt;
&lt;label asp-for=&quot;ADR.DeferralExpiry&quot; class=&quot;form-label&quot;&gt;&lt;/label&gt;
&lt;kendo-datepicker for=&quot;ADR.DeferralExpiry&quot; class=&quot;form-control deferred&quot;&gt;&lt;/kendo-datepicker&gt;
&lt;span asp-validation-for=&quot;ADR.DeferralExpiry&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;d-grid gap-2 d-md-flex justify-content-md-end&quot;&gt;
&lt;a class=&quot;btn btn-outline-secondary&quot; asp-page=&quot;./Index&quot;&gt; Close&lt;/a&gt;
&lt;input type=&quot;submit&quot; id=&quot;Submit&quot; value=&quot;Submit&quot; class=&quot;btn btn-primary&quot; /&gt;
&lt;/div&gt;
&lt;/form&gt;

Header:

&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot; /&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
&lt;title&gt;@ViewData[&quot;Title&quot;] - FlightLog&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;~/lib/bootstrap/dist/css/bootstrap.min.css&quot; /&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;~/css/site.css&quot; asp-append-version=&quot;true&quot; /&gt; 
&lt;script src=&quot;~/lib/jquery/dist/jquery.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;~/lib/bootstrap/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;~/lib/jquery-validation/dist/jquery.validate.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js&quot;&gt;&lt;/script&gt;    
&lt;link href=&quot;https://kendo.cdn.telerik.com/2022.2.621/styles/kendo.common-bootstrap.min.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
&lt;link href=&quot;https://kendo.cdn.telerik.com/2022.2.621/styles/kendo.bootstrap.min.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt; 
&lt;script src=&quot;https://kendo.cdn.telerik.com/2022.2.621/js/kendo.all.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://kendo.cdn.telerik.com/2022.2.621/js/kendo.aspnetmvc.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://kendo.cdn.telerik.com/2022.2.510/js/cultures/kendo.culture.en-NZ.min.js&quot;&gt;&lt;/script&gt;       
&lt;/head&gt;

答案1

得分: 1

以下是翻译好的部分:

<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<script>
    $.validator.setDefaults({
        validClass: "is-valid",
        // set error class doesn't work here, so I have to try with hard code for error class
        //errorClass: "is-invalid",

        highlight: function (element, errorClass, validClass) {
            $(element).addClass("is-invalid").removeClass(validClass);
        },
        unhighlight: function (element, errorClass, validClass) {
            $(element).addClass(validClass).removeClass("is-invalid");
        },
    });
</script>

Result:

[![enter image description here][1]][1]

注意:代码部分未翻译,保持原样。

英文:

You may try as below:

&lt;script src=&quot;~/lib/jquery/dist/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;~/lib/bootstrap/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;~/lib/jquery-validation/dist/jquery.validate.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
$.validator.setDefaults({
validClass: &quot;is-valid&quot;,
// set error class doesn&#39;t work here ,so i have to try with hard code for error class
//errorClass: &quot;is-invalid&quot;,
highlight: function (element, errorClass, validClass) {
$(element).addClass(&quot;is-invalid&quot;).removeClass(validClass);
},
unhighlight: function (element, errorClass, validClass) {
$(element).addClass(validClass).removeClass(&quot;is-invalid&quot;);
},
});

</script>

Result:

JQuery 表单验证

答案2

得分: 0

使用.setDefaults中的highlightunhighlight函数会在提交按钮使用之前触发验证。

对我而言的解决方案是在验证器和不显眼设置中都设置自定义类。

const validationCustomSettings = {
    validClass: "is-valid",
    errorClass: "is-invalid",
};

jQuery.validator.setDefaults(validationCustomSettings);
jQuery.validator.unobtrusive.options = validationCustomSettings;
英文:

Using the highlight and unhighlight functions within .setDefaults causes the validation to fire before the submit button is used.

The solution for myself was instead to set the custom classes in both the validator and unobtrusive settings.

const validationCustomSettings = {
validClass: &quot;is-valid&quot;,
errorClass: &quot;is-invalid&quot;,
};
jQuery.validator.setDefaults(validationCustomSettings);           
jQuery.validator.unobtrusive.options = validationCustomSettings;

huangapple
  • 本文由 发表于 2023年7月18日 08:24:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定