Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

460
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda