Skip to content

Instantly share code, notes, and snippets.

@flourigh
Last active July 3, 2020 13:11
Show Gist options
  • Save flourigh/204062839dc65f4d7155697350e5bc9e to your computer and use it in GitHub Desktop.
Save flourigh/204062839dc65f4d7155697350e5bc9e to your computer and use it in GitHub Desktop.
Validadores
/** consumidor_nascimento **/
pattern = /^(?:(?:31([-/.]?)(?:0[13578]|1[02]))\1|(?:(?:29|30)([-/.]?)(?:0[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)\d{2})$|^(?:29([-/.]?)02\3(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26]))))$|^(?:0[1-9]|1\d|2[0-8])([-/.]?)(?:(?:0[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)\d{2})$/i
if (!pattern.test(dados.valor)) {
this[dados.formulario].consumidor[dados.input] = 'Data inválida'
return
}
let day = Number(dados.valor.substring(0, 2))
let month = Number(dados.valor.substring(2, 4))
let year = Number(dados.valor.substring(4, 8))
let date = new Date()
let monthLength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if (year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0)) { monthLength[1] = 29 }
if (year < date.getFullYear() - 110 || month === 0 || month > 12) { Breack = 'Data inválida' }
if (year > date.getFullYear() - 18) { Breack = 'Você deve ser maior de 18 anos' }
if (date.getFullYear() - year === 18 && date.getMonth() + 1 < month) { Breack = 'Você deve ser maior de 18 anos' }
if (
date.getFullYear() - year === 18 &&
date.getMonth() + 1 === month &&
date.getDate() < day
) { Breack = 'Você deve ser maior de 18 anos' }
this[dados.formulario].consumidor[dados.input] =
Breack ||
(day > 0 && day <= monthLength[month - 1]) ||
'Data inválida'
if (Breack) return
/** consumidor_nascimento **/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment