【问题标题】:Form Validation in Date-Picker Field Not Working Properly日期选择器字段中的表单验证无法正常工作
【发布时间】:2019-05-08 02:30:21
【问题描述】:

我有以下 2 个用于日期选择器的字段。

鉴于:

<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>

当我单击“提交”按钮时,它会使用“必需”属性很好地呈现“inputForm”的所有字段,就像每当我将这些字段保留为空然后单击“提交”时一样,“必需”属性效果很好。但在那之后,如果我从 Date-Picker 字段中选择一个日期,它不会删除 Case File Date & Hearing Date 字段的“此字段是必需的”。

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");
        }
    });

提交按钮代码:

<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");
                    });
            }

        });

[添加图片以便更清楚地说明]

[在填写任何输入字段并单击提交按钮之前] []1

[填写输入字段值后,未删除案例归档日期和听证日期所需的消息] []2

请帮我解决这个问题。我只想在这些字段为空时显示“此字段为必填项”消息,并在这些字段具有从日期选择器中选择的值时隐藏此消息。

【问题讨论】:

  • submit_button 的其余代码在哪里?
  • @Viqas 代码为 submit_button 添加

标签: javascript validation datepicker dropdown required


【解决方案1】:

你的问题不是很清楚,你是否使用模型绑定与 MVC Razor 视图?

我想你可以使用下面的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(); 
    }
});

【讨论】:

  • 太棒了!奇迹般有效。如果这些字段为空,您能否建议我如何再次添加该错误消息。比如: $('#iCaseFileDate').on('change', function () { if ($('#iCaseFileDate').val().length>0) { $('#iCaseFileDate').removeAttr(" required"); $(this).next('.your_validation_message_div').remove(); } else{ // 不知道如何重新添加那个错误类 // $(this).next('.error ').add(); } });
  • 使用 $('#iCaseFileDate').attr("required");在 if 条件之前。如下所示 $('#iCaseFileDate').on('change', function () { $(this).attr("required"); if ($('#iCaseFileDate').val().length>0) { $('#iCaseFileDate').removeAttr("required"); //comment //找到包含验证消息的div *该字段是必需的*并删除它 //如下 $(this).next('.your_validation_message_div ').remove(); } });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-02
  • 2015-01-18
  • 1970-01-01
  • 1970-01-01
  • 2013-11-10
相关资源
最近更新 更多