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

92
Visualizações
Year Validation

Helo,

I have an input to enter year for the credit card expiry date How can i validate this input for an invalid 4 digits year and should accept only 4 digits onchange and should be equeal or greater then current year

const [year, setYear] = useState('');

function onSubmit(e) {
        e.preventDefault();
        dispatch(
            addPayment({
                card_holder: name,
                card_number: creditCardNo,
                expiration_month: month,
                expiration_year: year,
                card_type: creditCardType,
                cvc: cvv,
            }),
        );
        setCreditCardNo('');
        setName('');
        setCVV('');
        setMonth('');
        setYear('');
    }

 <div className={styles.sizeExtraSmall}>
    <input
     placeholder="Year"
     onChange={(e) => setYear(e.target.value)}
     required
     value={year}
     type="number"
   />
   <CreditCardSVG className={styles.profileIcons} />
 </div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here are two approaches, one using regex and the other using a function that takes a year and interval of acceptable years out:

const
    years = [1900, 2000, 25000, 2022, 2025, 2030],
    regex = /20[2-9]\d/;
    current_year_or_greater_with_interval = 
         (interval) => (year) => (year >= new Date().getFullYear()) 
                              && (year <= new Date().getFullYear() + interval);
    interval = 3;

for (year of years) {
    console.log(year, current_year_or_greater_with_interval(interval)(year));
    console.log(year, regex.test(year));
}

Or a bit cleaner and easier to read:

const 
    now = new Date().getFullYear(),
    years = [1900, 2000, 25000, 2022, 2025, 2030];

for (year of years) {
    console.log(`---${year}---`);
    console.log('expr (+ 100)', year >= now && year < now + 100);
    console.log('regex', /20[2-9]\d/.test(year));
}

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