the trick I came up with to validate comma separated input of emails with parsleyjs was to create a custom validator for a text field and rely on the browser's native support for validating multiple emails:
<input type="text" name="emails" required data-parsley-multipleemail>
(nb: we did not use <input type="email" multiple />
so this solution won't degrade to a proper email field.)
Parsley relies on the browser's native validators when available, so we'll do the same. (FYI using regexp for email validation is a pandoras box) Before loading Parsley.js, define a your custom validator like this:
window.ParsleyConfig = {
validators: {
multipleemail: {
fn: function (value) {
return $('<input type="email" multiple>').val(value)[0].checkValidity();
},
priority: 32
}
}
};
what this does is
- create a virtual email input element
- assign it the submitted value
[0]
gets us the native javascript element (ie without jquery)
- then we use the browsers native ValidityChecker which returns either true or false
- return the result
lastly, insert this code after you load parsley to create a custom validation message (in english) for this validator:
window.ParsleyValidator && window.ParsleyValidator.addMessage('en', 'multipleemail', 'Invalid email(s)');