Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

REGEX for Email Address validates with incomplete email addresses #10052

@benrondeau

Description

@benrondeau

When using the REGEX included in Angular for validating email addresses, it validates addresses without a finished domain ( user@domain will validate the same as user@domain.com ). Therefore an incomplete email address passes validation when it shouldn't.

Steps to reproduce:

  1. Create HTML page with angular.js , a <input type="email" required> , and some form of validation using $error or $invalid . Example is below.
  2. Input the email address user@domain into the input field.
  3. Validation will pass using angular's validation REGEX located here.

Live Example on Plunkr

Code Example:

<!DOCTYPE html>
<html>

  <head>
    <script src="//code.angularjs.org/1.3.1/angular.js"></script>
  </head>

  <body>
    <div ng-app>
    <h3>Email Address Validation with Angular</h3>
      <form name="contactForm" novalidate>
          <label for="emailaddress">Email Address:</label>
          <input type="email"
            name="emailaddress"
            ng-model="user.emailaddress"
            required>
      </form>
      <!--Error Messages for Email Field-->
          <div style="color:red; font-weight:bold;margin-top:20px;" ng-show="contactForm.emailaddress.$invalid">
              ERROR. Your email address does not validate with current Angular EMAIL_REGEXP.
          </div>
      </div>
  </body>

</html>

Current REGEX used:

var EMAIL_REGEXP = /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;

Proposed REGEX change. This conforms to REGEX standard RFC 5322. Live Example:

var EMAIL_REGEXP = /[a-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g;

I tried changing the EMAIL_REGEXP to the proposed value above but when I ran the test suite, I received the following error:

Chrome 38.0.2125 (Mac OS X 10.10.0) input email EMAIL_REGEXP should validate email FAILED
    Expected false to be true.
    Error: Expected false to be true.
        at null.<anonymous> (/Users/benrondeau/Desktop/angularfix/angular.js/test/ng/directive/inputSpec.js:4081:49)
    Expected false to be true.
    Error: Expected false to be true.
        at null.<anonymous> (/Users/benrondeau/Desktop/angularfix/angular.js/test/ng/directive/inputSpec.js:4082:44)
    Expected false to be true.
    Error: Expected false to be true.
        at null.<anonymous> (/Users/benrondeau/Desktop/angularfix/angular.js/test/ng/directive/inputSpec.js:4087:42)

I am not knowledgeable enough about tests or Angular to figure out a fix that validates the test suite. Recommendations?

Thanks Angular team!!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions