Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

457
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda