Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate Multiple Email Addresses with HTML5

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

like image 892
Ben McCormick Avatar asked Oct 07 '13 17:10

Ben McCormick


People also ask

How do I verify multiple email addresses?

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.

How do I validate an email address in HTML?

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.

Which is the correct syntax for email validation in HTML 5?

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 "."

How do I add multiple email addresses in HTML?

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.


1 Answers

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.

like image 160
Ryan Avatar answered Sep 24 '22 05:09

Ryan