I'm trying to construct an email form that takes multiple comma separated emails as an input and verifies them using HTML5. I want to use the following regex to sanity check the input:
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b
Here's what I've tried
This doesn't seem to work for more than one:
<input type="email" pattern="\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b" value="" multiple>
This works, but only does the built in email validation, which seems to be something like .+@.+
.
<input type="email" value="" multiple>
Is there a way to mix pattern and multiple so the regex checks each item in the comma separated list?
I have a demo here: http://codepen.io/ben336/pen/ihjKA
You just parse the string into an array of string, each with a single email address. Split the string on your delimiter, get the array of string back and then enumerate the array, sending each email address to your code that validates the address.
The <input type="email"> defines a field for an e-mail address. The input value is automatically validated to ensure it is a properly formatted e-mail address. To define an e-mail field that allows multiple e-mail addresses, add the "multiple" attribute.
An <input> element with type="email" that must be in the following order: [email protected] (characters followed by an @ sign, followed by more characters, and then a "."
When set to true, it specifies that the user is allowed to enter more than one value/email in the email field. This property reflects the HTML multiple attribute. Tip: On submit, separate each email with a comma, like: [email protected], [email protected], [email protected] in the email field.
Try this:
<input type="email" multiple pattern="^([\w+-.%]+@[\w-.]+\.[A-Za-z]{2,4},*[\W]*)+$" value="">
Update:
Using <input type="email" value="" multiple>
does seem to be bugged by letting "a@b" pass. Though I would go with this method to keep the semantics as you said, and simply use validation on the server side as well just in case someone tries to submit an email like "a@b".
Just as a note, you should always validate on the server side for security reasons anyway, as client side validation can be bypassed very easily.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With