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

250
Vistas
How to check credit card expiration date?

The user fills in the credit card information. I want the validation to work if the user enters the expiration date before today. But the input is as follows; mm/yy . However, the user enters the last 2 digits of the year. For example, let's say the user enters the expiration date as 06/21. Today's date is 06/22. In this case validation should work. I need to check the month and year the user entered. I want to show error message if user enter wrong date. I've been searching but can't integrate it.

How can I solve this problem?

const [errorList, setErrorList] = useState({
  expDateValidationStateError: true,
});

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

const expDateOnChange = (event) => {
  if (expDateValidator(event.target.value)) {
    setExpDateValidationState({ error: false, helperText: '' });
    setErrorList({ ...errorList, expDateValidationStateError: false });
  } else {
    setExpDateValidationState({ error: true, helperText: t('payment-credit-card.error-messages.exp_date') });
    setErrorList({ ...errorList, expDateValidationStateError: true });
  }
};

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

0

Here is the code:

const ccExpired = (date)=>{
    const dateArr = date.split('/');
    const exYear = +dateArr[1] + 2000;
    const exMonth = +dateArr[0] - 1; // JS months start with 0

    const exDay = new Date(exYear,exMonth + 1,0).getDate();
    const dateObj = new Date(exYear,exMonth,exDay - 2); // Timezones fix.
    const dateNow = new Date();
    return dateNow > dateObj;
}
console.log("2/22 " + ccExpired('2/22'));
console.log("3/33 " + ccExpired('3/33'));

//This is for SO debug only, ignore.
const currentMonth = (new Date().getMonth() + 1);
const currentYear = (new Date().getFullYear()).toString().replace(/^20/, '');
const thisMonth = `${currentMonth}/${currentYear}`;
const lastMonth = `${(currentMonth-1)}/${currentYear}`;
const nextMonth = `${(currentMonth+1)}/${currentYear}`;
console.log(thisMonth + " " + ccExpired(thisMonth));
console.log(lastMonth + " " + ccExpired(lastMonth));
console.log(nextMonth + " " + ccExpired(nextMonth));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Or perhaps

const isCCExpired = str => {
  const [mm,yy] = str.split("/");
  const dateObj = new Date(+yy + 2000,mm-1,15); // months are 0 based and don't take the 1st due to timezones
  console.log(dateObj);
  return dateObj.getTime() < new Date().getTime();
};
console.log("3/22",isCCExpired('3/22'));
console.log("3/23",isCCExpired('3/23'));

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