2

Having this HTML (jsfiddle):

<form>
    <input name="email" type="email" placeholder="Email" required />
    <input type="submit" value="Submit">
</form>

Why is hello@world not being flagged as an invalid email address?

Tried using pattern="/^\w+(?:\.\w+)*@\w+(?:\.\w+)+$/" but not much luck either.

Claudio Bredfeldt
  • 1,423
  • 16
  • 27
  • In general attempting to validate emails using regex is a bad idea. There are better ways to do it. – Terry Oct 11 '15 at 22:04
  • Did you get it work using the HTML5 `pattern` attribute? – Cyzanfar Oct 11 '15 at 22:59
  • @Cyzanfar, yes, it works - thanks! – Claudio Bredfeldt Oct 11 '15 at 23:13
  • 2
    Too many sites tell me that my email address is not valid _even though it is my **actual** email address_ that I use. Almost everyone who uses a regex to validate email gets it wrong, by making it too restrictive. The fact is, _everything_ to the left of the `@` is valid, even another `@` if it's escaped correctly. I suggest allowing the `input type="email"` to do its validation, then also check yourself there is a dot `.` somewhere. You almost _always_ want to send an email to the address to verify it anyway, in case someone types a valid-looking _but wrong_ address. – Stephen P Oct 11 '15 at 23:30

2 Answers2

6

From the HTML5 specifications :

A valid e-mail address is a string that matches the email production of the following ABNF, the character set for which is Unicode. This ABNF implements the extensions described in RFC 1123. [ABNF] [RFC5322] [RFC1034] [RFC1123]

email = 1*( atext / "." ) "@" label *( "." label )

label = let-dig [ [ ldh-str ] let-dig ] ; limited to a length of >63 characters by RFC 1034 section 3

atext = < as defined in RFC 5322 section 3.2.3 >

let-dig = < as defined in RFC 1034 section 3.5 >

ldh-str = < as defined in RFC 1034 section 3.5 >

Thus yes, hello@world is considered valid, because *("." label) does not force a . after the @

Community
  • 1
  • 1
Harijoe
  • 1,771
  • 1
  • 16
  • 27
  • Thanks for this. Why wouldn't be a `.` necessary after the `@` in order to specify a valid email address? Valid within some intranets? – Claudio Bredfeldt Oct 11 '15 at 21:47
  • 3
    Because an email address is of the form : local@domain -- and a domain does not necessarily have a dot (see the example of an address such as contact@localhost). Further reading on hostnames constraints here : https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names – Harijoe Oct 11 '15 at 21:50
  • 1
    And some TLDs have MX records on them, so presumably have email addresses at them. – gsnedders Oct 11 '15 at 22:05
  • 1
    +1 — This actually answers the OP's question _"Why is hello@world not being flagged as an invalid email address?"_ and bonus, it's _**correct!**_ – Stephen P Oct 12 '15 at 20:04
0

Try this pattern instead:

\b[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}\b

You can add a function that parses and validates the email string like so:

From this post.

function validateEmail(email) {
    var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    return re.test(email);
}

Here is a live example

HTML solution You can use the pattern attribute in HTML5 for validating emails:

<form>
        <input pattern="/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/" required /> 
        <br />
        <input type="submit" value="Submit Now!">
</form>

Here is the source: link

Community
  • 1
  • 1
Cyzanfar
  • 6,997
  • 9
  • 43
  • 81
  • If using JS is an option, then this answer works fine. Thanks! – Claudio Bredfeldt Oct 11 '15 at 21:50
  • If it can't be solved with HTML only, then yes, it does. – Claudio Bredfeldt Oct 11 '15 at 21:56
  • 1
    Yes you can solve this problem using HTML. I've updated my answer, take a look. – Cyzanfar Oct 11 '15 at 22:01
  • If you use something simple like `[A-Za-z0-9(etc)]` you will exclude accented characters, like in `josé@losbaños.org` which are perfectly valid in emails and domains. – Stephen P Oct 11 '15 at 23:35
  • @Cyzanfar Every single regular expression in this answer is wrong. – EKW Dec 02 '18 at 17:34
  • @Cyzanfar These fail nearly every single test case that isn't "foo@blah.com" or "foo+bar@blah.com" You're excluding non-Western languages. You're excluding IP addresses. You're excluding "foo=bar@blah.com". And, for that matter, you're excluding the EXTREMELY LONG list of TLDs longer than 6 characters. – EKW Dec 02 '18 at 17:42