I have the following 2 fields which are for 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. 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:
$(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
[After filling input field values, required message not removing for Case File Date & Hearing Date]
[]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?
I think you can use below jquery code
$('#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();
}
});