JQuery Validation plugin is ignored, elements are checked

I validate the form using tje jQuery Validation Plugin. If the browser supports the local HTML5 datetime element, I want this input not to be checked. Everything works fine, except that elements with the ignore class are still checked.

HTML in Chrome (with datetime-local support)

<form action="/Events/EditEventInfo/37" id="EditForm" method="post" novalidate="novalidate"><input name="__RequestVerificationToken" type="hidden" value="bg5O93sGSgPSWzzsaMfKzd0FPWddBBw9ZYC4srs5xBWmJgsBKWjmD2TL0OXyQNwGl1fa7orAp08pCL1RLxzlSdbNWc9YUxxd1rxGrpw0fhgINRnd3vXoCzG5bDhe2ySk77kXfCfyEJvy-uvYRIbA8Q2">    <div class="form-horizontal">
    <div class="form-group">
        <label for="StartDate" class="control-label col-md-2">Startdatum</label>
        <div class="col-md-10">
            <input id="StartDate" name="StartDate" class="form-control ignore error" type="datetime-local" required="" aria-required="true" aria-invalid="true"><label id="StartDate-error" class="error" for="StartDate">Bitte geben sie ein Datum im Format 'dd.MM.yyyy HH:mm' an.</label>
        </div>
    </div>

    <div class="form-group">
        <label for="EndDate" class="control-label col-md-2">Enddatum</label>
        <div class="col-md-10">
            <input id="EndDate" name="EndDate" class="form-control ignore error" type="datetime-local" required="" aria-required="true"><label id="EndDate-error" class="error" for="EndDate">Bitte geben sie ein Datum im Format 'dd.MM.yyyy HH:mm' an.</label>
        </div>
    </div>

    <div class="form-group">
        <label for="Name" class="control-label col-md-2">Name</label>
        <div class="col-md-10">
            <input id="Name" name="Name" class="form-control valid" value="Party. Yolo." type="text" minlength="2" required="" aria-required="true">

        </div>
    </div>       
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Speichern" class="btn btn-default">
        </div>
    </div>
</div>

Script:

<script type="text/javascript">    
$.validator.addMethod(
"deDateTime",
function (value, element) {
    //dd.MM.yyyy HH:mm
    var re = /^\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}$/;
    return (this.optional(element) && value == "") || re.test(value);
},
"Bitte geben sie ein Datum im Format 'dd.MM.yyyy HH:mm' an."
);
if (DateTimeLocalSupport())
{
    $('#StartDate').val("@Model.StartDate.ToString("s")");
    $('#EndDate').val("@Model.EndDate.ToString("s")");
    //HTML5 input types are available, no need to validate those fields
    $('#StartDate').addClass("ignore");
    $('#EndDate').addClass("ignore");
}
else
{
    $('#StartDate').val("@Model.StartDate.ToString("dd.MM.yyyy HH:mm")");
    $('#EndDate').val("@Model.EndDate.ToString("dd.MM.yyyy HH:mm")");
}
$('#EditForm').validate({
    ignore: ".ignore :hidden",
    rules: {
        StartDate: {
            deDateTime: true
        },
        EndDate: {
            deDateTime: true
        }
    }
});
alert("Valid: " + $('#EditForm').valid());

</script>

Testing HTML5 support, filling in fields, and adding classes is great. Only the validation plugin does not yet validate elements that are not intended.

Decision:

ignore: '.ignore, :hidden'
+4
source share
1 answer

ignore: ".ignore :hidden"tells him to ignore hidden fields with the class ignore.

ignore: ".ignore" , .ignore.

ignore: ".ignore, :hidden" , .ignore AND .


ignore ignore: ":hidden", .

ignore: [] .

+15

Source: https://habr.com/ru/post/1547008/


All Articles