So, to prevent entering text (and to accept only date-time values), I implemented validation using the following JavaScript code:
โค้ด: เลือกทั้งหมด
var startDateStr = document.querySelector('[name="jform[event_start_date]"]').value;
var validFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2})?$/;
var startDate = new Date(startDateStr);
if (!validFormat.test(startDateStr) || isNaN(startDate.getTime())) {
alert("The start date value is invalid. Please enter a valid date and time in the format YYYY-MM-DDTHH:MM or YYYY-MM-DDTHH:MM:SS.");
e.preventDefault();
return false;
}
And it displays an alert box if text is entered.
However, when the alert box is closed, the entered text disappears, and the current date-time is automatically filled in the form.
Screenshot: text disappears and today date-time auto filled
How can I fix it to display the entered text, or is there another solution to allow users to enter only date-time values?
The main purpose is to ensure only date-time values are accepted.