Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

452
Views
How to auto add slash(/) in expiry date field input javascript

I wanna auto add slash in credit card expiry date field input. I wanna add slash after type 2 character and remove slash after delete third digit. Example, 23/ (auto add slash after type number 3) 23/4 (auto remove slash after delete number 4)

addSlashes(elementID) {
    let ele = document.getElementById(elementID)
    const value = ele.value
    let finalVal = null
    if (value.length === 2) {
       finalVal = `${value}/`
    }
    document.getElementById(elementID).value = finalVal
},
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As soon as the / is added after 2 characters, the total length becomes 3. So, it is not very clear how the operation should happen.

However, if this is for some automatic changes to happen when progressive typing takes place, here is the code to add a / automatically when the user has typed 2 characters and remove it when the next character is added after the /

Edit OP needs to delete the / when the 3rd digit is removed.

function modifyInput(ele) {
  
  if (ele.value.length === 2)  
    ele.value = ele.value + '/'
  else    
    if (ele.value.length === 3 && ele.value.charAt(2) === '/')
      ele.value = ele.value.replace('/', '');
}
<input type="text" onkeyup="modifyInput(this)">

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!