สอบถามฟังกชั่นวันที่ให้ใส่ / ให้เลยเวลาคีย์วันที่ต้องทำอย่างไรค่ะ

พูดคุยแลกเปลี่ยน ปัญหา การเขียน JavaScript เครื่องมือ AJAX Web 2.0 AJAX Framework jQuery และ Node.JS รวมถึง Framework Express ของ Node.JS ทำงานฝั่ง Server

Moderator: mindphp

ภาพประจำตัวสมาชิก
eange08
PHP VIP Members
PHP VIP Members
โพสต์: 10212
ลงทะเบียนเมื่อ: 22/12/2020 10:09 am

สอบถามฟังกชั่นวันที่ให้ใส่ / ให้เลยเวลาคีย์วันที่ต้องทำอย่างไรค่ะ

โพสต์ โดย eange08 »

ทำช่องใส่วันที่หมดอายุแบบ mm/yyyy อยากให้เวลาคีย์ mm แล้วขึ้น / ให้เลยจะต้องเขียนอย่างไรค่ะ
ถามใน openai ยัง งงกับการเอามาแปลงใช้งานอยู่ค่ะ

โค้ด: เลือกทั้งหมด

$(document).ready(function() {
  var maxLength = 100; // Set the maximum length of the input field
  $('#text-input').on('input', function() {
    var length = $(this).val().length; // Get the length of the user input
    var remaining = maxLength - length; // Calculate the remaining characters
    $('#char-count').text(remaining); // Update the character count display
  });
});
ช่องวันหมดอายุ

โค้ด: เลือกทั้งหมด

<input name='package_noti_age' id='package_noti_age' type='text' class='input-short' maxlength='7'>
อยากให้ใส่รูปแบบนี้นะคะ
Selection_999(1110).png
Selection_999(1110).png (5.54 KiB) Viewed 224 times
ภาพประจำตัวสมาชิก
tsukasaz
PHP VIP Members
PHP VIP Members
โพสต์: 19052
ลงทะเบียนเมื่อ: 18/04/2012 9:39 am

Re: สอบถามฟังกชั่นวันที่ให้ใส่ / ให้เลยเวลาคีย์วันที่ต้องทำอย่างไรค่ะ

โพสต์ โดย tsukasaz »

ลองดู code นี้นะครับ


HTML

โค้ด: เลือกทั้งหมด

<label for="date-input">Enter date (dd/yyyy): </label>
<input type="text" id="date_input" pattern="\d{2}/\d{4}" placeholder="dd/yyyy" maxlength="7">
JS

โค้ด: เลือกทั้งหมด

$(document).ready(function(){
  $('#date_input').keyup(function() {
    const inputValue = $(this).val().replace(/\D/g, "");
    const formattedValue = inputValue.replace(/(\d{2})(\d{4})/, "$1/$2"); 
    $(this).val(formattedValue);
  });
});
The last bug isn't fixed until the last user is dead. (Sidney Markowitz, 1995)
ตอบกลับโพส

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

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