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

200
Vistas
Unable to delete text from input after adding regex to it

I've this javascript code:

document.querySelector('div[data-field-name="CNPJ"] input').addEventListener('keydown', (inheritance) => {
        let value = inheritance.target.value;
        console.log(value.length)
        if (value.length >= 18) {
            inheritance.preventDefault()
            inheritance.stopPropagation()
            return
        }
        value = value.replace(/\D/g, "")
        value = value.replace(/^(\d{2})(\d)/, "$1.$2")
        value = value.replace(/^(\d{2}).(\d{3})(\d)/, "$1.$2.$3")
        value = value.replace(/.(\d{3})(\d)/, ".$1/$2")
        value = value.replace(/(\d{4})(\d)/, "$1-$2")
        inheritance.target.value = value;
    })

The problem that I'm having is when the input's value reach the maximum length (I stipulated in the code), I'm unable to delete the text.

I don't know what is causing this issue.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You are preventing default input behaviour in this line when you reach the max length. Remove it and add a maxlength check in the HTML code instead.

  if (value.length >= 18) {
      inheritance.preventDefault()
      inheritance.stopPropagation()
      return
  }

I am not sure why this line is required since you can do something similar in HTML:

<input maxlength="18"></input>

Can you elaborate why maxlength has to be done in JavaScript?

Alternatively you can add a Backspace or similar key checks in your logic:

if (
    value.length >= 18
    && inheritance.key !== "Backspace"
  ) {
    inheritance.preventDefault()
    inheritance.stopPropagation()
    return
}

I don't recommend detecting specific keys personally since HTML considers a lot more edge cases than a single key check will.

Check out the MDN doc on input pattern.

about 4 years ago · Juan Pablo Isaza Denunciar

0

There is no way for the previous implementation cause inheritance.preventDefault() makes the function not to produce an input when the length is greater than or equal to 18

The better way is to scratch that if statement then add maxlength="18" attribute to the input element.

See the demo :

That was because of the if statement

document.querySelector('div[data-field-name="CNPJ"] input').addEventListener('keydown', (e) => {
  let value = e.target.value;
  value = value.replace(/\D/g, "")
  value = value.replace(/^(\d{2})(\d)/, "$1.$2")
  value = value.replace(/^(\d{2}).(\d{3})(\d)/, "$1.$2.$3")
  value = value.replace(/.(\d{3})(\d)/, ".$1/$2")
  value = value.replace(/(\d{4})(\d)/, "$1-$2")
  e.target.value = value;
})
<div data-field-name="CNPJ">
  <input type="text" maxlength="18" />
</div>

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