-2

I have a form which I need to verify internet routable email addresses before submission and deny local routing. Both ng-pattern="email.text" and ng-pattern="email"pass bob@bob which should fail, as these are customer email addresses outside of the local network.

The following do not give me what I'm looking for (many are using rudimentary RegExs that don't fully conform to RFC standards or don't deny local routing):

Reading http://www.regular-expressions.info/email.html it references RFC 5322 with regular expression:

\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*
  |  "(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]
      |  \\[\x01-\x09\x0b\x0c\x0e-\x7f])*")
@ (?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
  |  \[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
       (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:
          (?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]
          |  \\[\x01-\x09\x0b\x0c\x0e-\x7f])+)
     \])\z

Is there a way to make the above into a RegEx that requires a tld-type, so it is internet routable? Specifically, where I can use it in a ng-pattern, like ng-pattern="<regex goes here>" in the form?

Community
  • 1
  • 1
James Oravec
  • 19,579
  • 27
  • 94
  • 160
  • 2
    why should `bob@bob` be invalid? not all email has to be "internet routable". all bob@bob says is that on the local network there's a machine named bob, with a user named bob. just because you can't use that outside of that particular network doesn't mean it's invalid. – Marc B Feb 22 '16 at 18:25
  • 1
    Replace `\A` with `^`, `\z` with `$` and use `/..../` notation (and `"` must be `\x22` and `'` can be `\x27`). Check [this demo](https://regex101.com/r/dI2yI5/2) – Wiktor Stribiżew Feb 22 '16 at 18:26
  • Stop trying to validate all emailaddresses with regex. I haven't ever seen a solution which passes all tests. Check for an `@` and an `.` in the domain if you need it and verify by sending a mail if you really want to check. – PeeHaa Feb 22 '16 at 18:27
  • @MarcB The information is really good, it explains the frustration I was having. Is there a way to check for an "internet routable" email address in angular? Or other regex that I could use to ensure it is internet routable? – James Oravec Feb 22 '16 at 18:34
  • technically speaking, any email address for "outside" would need to have at least a @domain.tld-type setup. e.g. gmail.com. `bob@gmail` and `bob@gmail.com` would be two different things. `bob@gmail` is a local address, where you've got some machine named gmail in your local network. bob@gmail.com would go to google's gmail servers, as expected. – Marc B Feb 22 '16 at 18:37
  • just like doing `nslookup foo` at your command prompt would actually append the local search domains, so you'd really be doing a lookup on `foo.domain.tld`. email can do the same thing. – Marc B Feb 22 '16 at 18:37
  • Is there something wrong with [``](http://diveintohtml5.info/forms.html#type-email)? – Stephen P Feb 22 '16 at 20:53
  • Possible duplicate of [Validate email address in JavaScript?](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – Stephen P Feb 22 '16 at 21:08
  • @StephenP, yes, it passes `bob@bob` which I do not wish to allow. I don't want local routing, only internet routing, as this is a validation for customer email addresses, so they would not be on the same network. So the suggested duplicate is not valid. – James Oravec Feb 23 '16 at 15:30

2 Answers2

1

I like this one from here
http://www.w3.org/TR/html5/forms.html#valid-e-mail-address

/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/  

Expanded

 ^ 
 [a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+ 
 @
 [a-zA-Z0-9] 
 (?:
      [a-zA-Z0-9-]{0,61} 
      [a-zA-Z0-9] 
 )?
 (?:
      \. 
      [a-zA-Z0-9] 
      (?:
           [a-zA-Z0-9-]{0,61} 
           [a-zA-Z0-9] 
      )?
 )*
 $ 
-1

Wiktor's reg ex in the requested format, passes internet routable email addresses and denies local routing:

ng-pattern="/^(?:[a-z0-9!#$%&\x27*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\x27*+/=?^_`{|}~-]+)*|\x22(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\x22)@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/"
James Oravec
  • 19,579
  • 27
  • 94
  • 160
  • Don't waste your time with this stupid pattern (that is false and probably slow). Use a basic check. Keep in mind that whatever the pattern you use, it doesn't prevent to enter an email that doesn't exist (even it is well formatted). So the problem stays the same. – Casimir et Hippolyte Feb 22 '16 at 19:06
  • The question is about the text being `well formatted`, which helps avoid user entry problems. Which is what is desired in the question. Per your argument, even checking if a domain exists, it doesn't mean that the email address is legit either, so the only real way to check would be to send an email and have them verify they received it via a randomly generate link (just sending isn't enough because servers can be setup to not send bounce backs). Verification emails could be a separate process of validation (but is outside the scope of the question). – James Oravec Feb 23 '16 at 15:45