Q - How to prevent from auto date, time selection in calendar input field of Joomla 5 custom component ?

Moderator: mindphp

Raja Pdl
PHP VIP Members
PHP VIP Members
โพสต์: 2241
ลงทะเบียนเมื่อ: 27/05/2024 9:50 am

Q - How to prevent from auto date, time selection in calendar input field of Joomla 5 custom component ?

โพสต์ที่ยังไม่ได้อ่าน โดย Raja Pdl »

In my Joomla custom component, the calendar input fields can accept text.
abc.PNG
abc.PNG (3.12 KiB) Viewed 75 times


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
auto refill.PNG
auto refill.PNG (4.53 KiB) Viewed 75 times

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.
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 3