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

149
Visualizações
automatic slash when 2 digits are entered | react

There is a credit card component. It asks the user to enter credit card information. However, I want to automatically put a slash between the day and month when the user enters the credit card expiration date. I searched the expiration date entry as "auto slash when 2 digits are entered" but I haven't been successful yet.

i can write; 0614

The format i want; 06/14

How can I solve it?

js

const [expDateValidationState, setExpDateValidationState] = useState({
  error: false,
  helperText: '',
});

const expDateOnChange = (event) => {
  if (expDateValidator(event.target.value)) {
    setExpDateValidationState({ error: false, helperText: '' });
    setPaymentInfo({
      ...paymentInfo,
      expDate: event.target.value === '' ? null : event.target.value,
    });
  } else {
    setExpDateValidationState({
      error: true,
      helperText: 'Please enter your expire date.',
    });
    setPaymentInfo({
      ...paymentInfo,
      expDate: null,
    });
  }

const handleExpDateChange = (event) => {
  expDateOnChange(event);
  handleInputChange(event);
};

validator

export const expDateValidator = (expDate) => {
  const expDateRegex = /^(0[1-9]|1[0-2])\/?([0-9]{4}|[0-9]{2})$/;

  return expDateRegex.test(expDate);
};

html

<AS.TextField
  placeholder="aa/YY"
  inputProps={{ maxLength: 5 }}
  onChange={handleExpDateChange}
  error={expDateValidationState.error}
  helperText={expDateValidationState.helperText}
  name="expDate"
  value={paymentInfo.expDate}
/>
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

What you're looking for is a masked input or a similar struct.

I'm not sure what allowances you've got for your design, but these libs might have what you need:

https://www.npmjs.com/package/credit-card-expiry-date

https://www.npmjs.com/package/react-input-mask

about 4 years ago · Santiago Gelvez 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