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

163
Visualizações
ReactJS - replace / expire date card

When entering 4 digits, you need to make a substitution and set a slash between the second and third digits. For example, I enter 1225 - I need get 12/25

changeInput(event) {
this.setState({ [event.target.name]: event.target.value.replace(/\/?/g, '').replace(/(\d{2})/, '$1/') });
}

<input type="text" inputMode="numeric" id="cardDate" name="cardDate" value={this.state.cardDate} autoComplete="off" maxLength="5" placeholder="MMYY" onChange={this.changeInput.bind(this)} />


I wrote a replacement code, but when I try to edit the input field, there is a problem. What's wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Using Regex for this is a little bit overkill, there are a few ways to do this: First, using String.prototype:

 String.prototype.insert = function (index, string) {
      if (index > 0) {
        return this.substring(0, index) + string + this.substr(index);
      }
    
      return string + this;
    };
    
const string = "1234";
const substring = string.insert(2, "/");
console.log(substring) // "12/34"

You can do it through an array too:

function replaceAt(string, index, toInsert) {
  const substring = [...string];
  substring.splice(2, 0, toInsert);
  return substring.join("");
}

const string = "1234";
const replaced = replaceAt(string, 1, "/");
console.log(replaced);
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