Home > Mobile >  how can i validate data in dd-mm-yyyy format
how can i validate data in dd-mm-yyyy format

Time:05-15

I have the following date "13-05-2022"

if (!validateDate(this.formGeral.value.data.singleDate?.formatted)) {
  this.errors.push('Data com formato inválido! Selecione a data de entrega novamente, se o problema persistir, favor entrar em contato com a nossa equipe.');
}

I use this validator, but it only validates in yyyy-mm-dd format

export function validateDate(date: string) {
  const regex = new RegExp('([0-9]{4}[-](0[1-9]|1[0-2])[-]([0-2]{1}[0-9]{1}|3[0-1]{1}))');
  return regex.test(date);
}

How could I validate a date with dd-mm-yyyy format?

CodePudding user response:

function validateDate(date) {
  const regex = new RegExp('([0-2]{1}[0-9]{1}|3[0-1]{1})[-](0[1-9]|1[0-2])[-]([0-9]{4})');
  return regex.test(date);
}

console.log(validateDate('2020-10-10')) // false
console.log(validateDate('10-10-2020')) // true

  • Related