简体   繁体   中英

Validate multiple fields with Parsley?

I have three text input fields used for entering a date. I'd like to validate these to make sure they are, together, a valid date. I can write JavaScript to test that something is a date, but I can't see a way to use Parsley.js to validate multiple fields in one go, and provide a single error message.

My fields are like this:

<input type="number" name="date-day" value="" maxlength="2">
<input type="number" name="date-month" value="" maxlength="2">
<input type="number" name="date-year" value="" maxlength="4">

From this answer I suspect the answer is that this isn't (simply) possible, but it seems like quite a common need, so I'd like to be 100% sure!

Here's roughly what I've ended up with. This is my HTML now:

<input type="number" name="dob-day" value="" maxlength="2" class="js-dob-day" required="required" data-parsley-date="js-dob">
<input type="number" name="dob-month" value="" maxlength="2" class="js-dob-month">
<input type="number" name="dob-year" value="" maxlength="4" class="js-dob-year">

I've added a unique class for each field, so I can get their values. And added required and data-parsley-date to the day input.

Here's my JavaScript to add a validator that checks the combined fields to see if they make a valid date. I've included two utility functions used within the validator, for completeness. The validator relies on moment.js to check the final string for validity.

// Is `n` a number or numeric string (eg "3")?
function isNumeric(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
};

// Pad `n` with `z` (or `0` by default) to `width`.
function zeroPad(n, width, z) {
    z = z || '0';
    n = n + '';
    return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
};


window.ParsleyValidator
    .addValidator(
        // Name of our validator.
        'date',

        // `value` will be the value entered into the day field.
        // `requirements` will be "js-dob" in our example.
        function(value, requirements) {
            var day     = $('.'+requirements+'-day').val(), // or value
                month   = $('.'+requirements+'-month').val(),
                year    = $('.'+requirements+'-year').val();

            if (isNumeric(day) === false
                || isNumeric(month) === false
                || isNumeric(year) === false) {
                return false;
            };

            day     = zeroPad(day, 2);
            month   = zeroPad(month, 2);
            year    = zeroPad(year, 4);

            // Use moment.js to make a date which we can then test for validity.
            var date = moment(year+'-'+month+'-'+day, 'YYYY-MM-DD');

            if (date.isValid()) {
                return true;
            } else {
                return false;
            };
        },

        // priority. Not sure how this works.
        34
    )
    .addMessage('en', 'date', "Enter a valid date");

Any suggestions more than welcome!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM