简体   繁体   English

日期选择器字段中的表单验证无法正常工作

[英]Form Validation in Date-Picker Field Not Working Properly

I have the following 2 fields which are for Date-Picker. 我有以下两个字段用于Date-Picker。

In view: 鉴于:

<div class="form-line">                                
     <input type="text" id="iCaseFileDate" name="CaseFileDate" placeholder="Case File Date*" class="datepicker form-control" required/>                                
</div>

<div class="form-line">
     <input type="text" id="iHearingDate" name="HearingDate" placeholder="Hearing Date*" class="datepicker form-control" required/>                                
</div>

When i click the Submit button, it renders all fields of 'inputForm' very well with 'required' attribute like whenever i keep these fields empty then click submit the 'required' attribute works well. 当我单击Submit按钮时,它会很好地呈现带有'required'属性的'inputForm'的所有字段,就像每当我将这些字段保留为空然后单击Submit时'required'属性都能正常工作一样。 But after that if i select a date from Date-Picker field, it doesn't remove 'This field is required' for Case File Date & Hearing Date field. 但是在那之后,如果我从“日期选择器”字段中选择一个日期,则不会删除“案件文件日期和听证日期”字段的“此字段是必需的”。

Javascript Code: JavaScript代码:

$(document).ready(function () {

    $('#submit_button').on('click', function () {
        $('#inputForm').valid()
    });
});

    $('#iCaseFileDate').on('change', function () {
        if ($('#iCaseFileDate').val()) {
            $('#iCaseFileDate').removeAttr("required");
        }
    });        

    $('#iHearingDate').on('change', function () {
        if ($('#iHearingDate').val()) {
            $('#iHearingDate').removeAttr("required");
        }
    });

Submit Button Code: 提交按钮代码:

<div class="modal-footer">
     <button type="submit" id="submit_button" class="btn btn-success waves-light" onclick="saveData()">SAVE</button>
     <button type="button" class="btn btn-warning waves-red" data-dismiss="modal">Close</button>
</div>

function saveData() {
            $("#inputForm").submit();
        }
        $("#inputForm").on("submit", function (event) {
            event.preventDefault();
            tinyMCE.triggerSave();
            var $this = $(this);

            var frmValues = $this.serialize();
            var isValid = $("#inputForm").valid();
            if (isValid == false) {

            }
            else {
                $.ajax({
                    type: 'POST',
                    url: '/ClientInfo/Save',
                    data: frmValues
                })
                    .done(function (result) {
                        if (result) {
                            alert(result.info);
                            clearInputFields();
                            $('#inputModal').modal('hide');
                            ReloadTable();
                        }
                    })
                    .fail(function (xhr) {
                        alert("error");
                    });
            }

        });

[Image added for better clarification] [添加图像以更好地进行说明]

[ Before filling any input field & clicking Submit Button ] [ [ 在填写任何输入字段并单击提交按钮之前 ] [ 在此处输入图片说明 ] 1 ] 1

[ After filling input field values, required message not removing for Case File Date & Hearing Date ] [ [ 填写输入字段值后,“案例文件日期和听证日期所需的消息不会删除 ] [ 在此处输入图片说明 ] 2 ] 2

Please help me solve this. 请帮我解决这个问题。 I just want to show 'This field is required' message whenever these fields are empty and hide this message whenever these fields have value selected from datepickers. 我只想在这些字段为空时显示“此字段为必填”消息,并在这些字段的日期选择器中选择了值时隐藏此消息。

It is not very clear from your question, are you using model binding with MVC Razor view? 从您的问题还不清楚,您是否在MVC Razor视图中使用模型绑定?

I think you can use below jquery code 我想你可以使用下面的jQuery代码

$('#iCaseFileDate').on('change', function () {
    if ($('#iCaseFileDate').val().length>0) {
        $('#iCaseFileDate').removeAttr("required");
//comment
//Find the div containing validation message * the field is required* and remove it 
//like below
    $(this).next('.your_validation_message_div').remove(); 

    }
});        

$('#iHearingDate').on('change', function () {
    if ($('#iHearingDate').val().length>0) {
        $('#iHearingDate').removeAttr("required");
 //comment
//Find the div containing validation message * the field is required* and remove it 
//like below
    $(this).next('.your_validation_message_div').remove(); 
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM