Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Date validators #1291

Open
Marco-Sulla opened this issue Mar 25, 2019 · 0 comments
Open

Date validators #1291

Marco-Sulla opened this issue Mar 25, 2019 · 0 comments

Comments

@Marco-Sulla
Copy link
Contributor

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",
    }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant