'how can i validate data in dd-mm-yyyy format
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?
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Pete Pearl |