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

90
Vistas
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 Respuestas
Responde la pregunta

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 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