I am using a jQuery inputmask on the following form element (named ‘Last_Reviewed_On’) of my ASP.NET web application:
<div class="col-sm-4 form-floating">
<input type="date" class="form-control d-flex" id="Last_Reviewed_On" name="Last_Reviewed_On">
<label for="Last_Reviewed_On">Last Reviewed</label>
</div>
The initialization of the inputmask in my Javascript is as follows:
$('#Last_Reviewed_On').inputmask({ alias: "datetime", inputFormat: "dd/mm/yyyy" });
So, the form element works very well. It shows a ‘mm/dd/yyyy’ mask, and there’s a calendar icon for the user to select a date as well. I can fetch the value entered by the user correctly. The problem is when I try to set the value of the form element using data from an existing record. I am using an $.ajax
call to a webhandler to fetch a record, which I then wish to display in the form. I am using the following code to populate the ‘Last_Reviewed_On’ form element:
$('#Last_Reviewed_On').val(data.Last_Reviewed_On);
Nothing appears in the form element though. All I see is the input mask. I know the data is there, since I used console.log
to inspect the value.
Any idea why this isn’t working? I use inputmask for other fields without issue, so I’m stumped on this one.
2
Answers
So the answer to this was to switch to a bootstrap datepicker control, using this code:
and the following jQuery to initialize:
It works well and prevents entry of invalid dates.
Quite sure for a label you use text() and not val() as you have.