guillaumepotier / Parsley.js

Validate your forms, frontend, without writing a single line of javascript
http://parsleyjs.org
MIT License
9.05k stars 1.32k forks source link

Date validators #1291

Open Marco-Sulla opened 5 years ago

Marco-Sulla commented 5 years ago
window.Parsley.addValidator('date', {
    validate: function(value, format) {
        var date = moment(value, format, true);
        return date.isValid();
    },
    messages: {
        en: 'It must be a valid date with format %s',
        it: 'Deve essere una data valida nel formato %s'
    }
});
window.Parsley.addValidator('datebeforenow', {
    validate: function(value, format) {
        var date = moment(value, format, true);

        // Trick to collaborate with date validator
        if (! date.isValid()) {
            return true;
        }

        return date.isBefore(moment());
    },
    messages: {
        en: "Date must be before now",
        it: "La data deve precedere la data attuale",
    }
});

window.Parsley.addValidator('dateafternow', {
    validate: function(value, format) {
        var date = moment(value, format, true);

        // Trick to collaborate with date validator
        if (! date.isValid()) {
            return true;
        }

        return date.isAfter(moment());
    },
    messages: {
        en: "Date must be after now",
        it: "La data deve esse posteriore alla data attuale",
    }
});