Regex to validate data yyyy / mm / dd?

1

I have tried several expressions, but I did not get the desired result.

How can I validate the date for yyyy/mm/dd ?

    
asked by anonymous 25.05.2016 / 17:08

2 answers

2

Example on JSFIDDLE . This way you can validate the date in the formats: yyyy-mm-dd , yyyy/mm/dd , yyyy.mm.dd

function data_valida(date)
{
    var matches = /(\d{4})[-.\/](\d{2})[-.\/](\d{2})/.exec(date);
    if (matches == null) {
        return false;
    }
    var dia = matches[3];
    var mes = matches[2] - 1;
    var ano = matches[1];
    var data = new Date(ano, mes, dia);
    return data.getDate() == dia && data.getMonth() == mes && data.getFullYear() == ano;
}

alert(data_valida('2016.05.26'));
    
25.05.2016 / 17:13
0

Function in javascript to validate mask and date value in Brazilian format.

if(validateDate("27/04/2009"))
  console.log("OK");
else
   console.log("Data incorreta");

function validateDate(data) {
        // Ex: 10/01/1985
        var regex = "\d{2}/\d{2}/\d{4}";
        var dtArray = data.split("/");

        if (dtArray == null)
            return false;

        // Checks for dd/mm/yyyy format.
        var dtDay= dtArray[0];
        var dtMonth = dtArray[1];
        var dtYear = dtArray[2];

        if (dtMonth < 1 || dtMonth > 12)
            return false;
        else if (dtDay < 1 || dtDay> 31)
            return false;
        else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31)
            return false;
        else if (dtMonth == 2)
        {
            var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
            if (dtDay> 29 || (dtDay ==29 && !isleap))
                return false;
        }
        return true;
    }
    
25.10.2017 / 22:43