JavaScript : Check dates

// Dates JJ/MM/AAAA, valides, (aujourd’hui – 2 ans) <= début <= fin <= (aujourd’hui + 4 ans)
function checkDatesDR()
{
    var isValid = true;
    var errorId = 0;
    document.forms[0].dateDebut.style.backgroundColor = ‘white’;
    document.forms[0].dateFin.style.backgroundColor = ‘white’;
  
    if(document.forms[0].dateDebut.value.length == 10 && checkDate(document.forms[0].dateDebut)) {
        if(!checkLimiteDate(document.forms[0].dateDebut, 24)
            || !checkLimiteDate(document.forms[0].dateDebut, -48)) errorId = 1;
    } else errorId = 1;
  
    if(document.forms[0].dateFin.value.length == 10 && checkDate(document.forms[0].dateFin)) {
        if(!checkLimiteDate(document.forms[0].dateFin, 24)
            || !checkLimiteDate(document.forms[0].dateFin, -48)) errorId = 2;
    } else errorId = 2;
  
    if(errorId == 0) {
        var dateDebut = stringToDate(document.forms[0].dateDebut.value);
        var dateFin = stringToDate(document.forms[0].dateFin.value);
        var differenceDates = Date.parse(dateFin) – Date.parse(dateDebut);
        if (differenceDates < 0) errorId = 3;
    }
  
    if(errorId == 1) {
        isValid = false;
        document.forms[0].dateDebut.style.backgroundColor = ‘#efcfcd’;
    }
    else if(errorId == 2) {
        isValid = false;
        document.forms[0].dateFin.style.backgroundColor = ‘#efcfcd’;
    }
    else if(errorId == 3) {
        isValid = false;
        document.forms[0].dateDebut.style.backgroundColor = ‘#efcfcd’;
        document.forms[0].dateFin.style.backgroundColor = ‘#efcfcd’;
    }
  
    return isValid;
}

——————————–

// On vérifie si la date en paramètre existe
function checkDate(myDateField)
{
    var isValid = true;
    var expression = new RegExp(“^(0[1-9]|[12][0-9]|3[01])[\\-\\/](0[1-9]|1[012])[\\-\\/](19|20)\\d\\d$”);
  
    if (!myDateField.value.match(expression)) {
        isValid = false;
        myDateField.style.backgroundColor = ‘#efcfcd’;
    } else {
        if(RegExp.$1 == 31 && (RegExp.$2 == 4 || RegExp.$2 == 6 || RegExp.$2 == 9 || RegExp.$2 == 11)) {
            isValid = false;
            myDateField.style.backgroundColor = ‘#efcfcd’;
        } else if(RegExp.$1 >= 30 && RegExp.$2 == 2) {
            isValid = false;
            myDateField.style.backgroundColor = ‘#efcfcd’;
        }
        else myDateField.style.backgroundColor = ‘white’;
    }
    return isValid;
}

——————————–

// Transforme une chaîne au format JJ/MM/AAAA en objet JavaScript Date
function stringToDate(myDateString)
{
    var myMMDDYYYYdate = myDateString.substring(3, 5) + “/” + myDateString.substring(0, 2) + “/” + myDateString.substring(6, 10);
    return new Date(myMMDDYYYYdate);
}

——————————– 

 
// Vérifie si la date ne dépasse pas la limite imposée
function checkLimiteDate(myDateField, myLimite)
{
    var isValid = true;      
    var dateAComparer = stringToDate(myDateField.value);
    var dateLimite = new Date();
    dateLimite.setMonth(dateLimite.getMonth() – myLimite);
    if(dateAComparer.getDate() != dateLimite.getDate() || dateAComparer.getMonth() != dateLimite.getMonth()
        || dateAComparer.getFullYear() != dateLimite.getFullYear()) {
        if(myLimite > 0 && dateAComparer < dateLimite) {
            isValid = false;
            myDateField.style.backgroundColor = ‘#efcfcd’;
        }
        else if(myLimite < 0 && dateAComparer > dateLimite) {
            isValid = false;
            myDateField.style.backgroundColor = ‘#efcfcd’;
        }
    }
    return isValid;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: