Regular expression to not allow spaces anywhere in the email

I am trying to validate the email address using a regular expression such that it throws an error if white spaces are added anywhere in the email. The current regex I am using is this:

<input type="email" class="form-control" name="username" ng-     model="username" id="email" placeholder="Email" ng-pattern='/^(([^<>()\    [\]\\.,;:\[email protected]"]+(\.[^<>()\[\]\\.,;:\[email protected]"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/' required />
<span ng-show="login.username.$error.pattern">This email format is invalid!     </span>

The problem is it allows me to add white spaces in the end. How can I modify it so that if I add a whitespace in the end the error message is fired?

Answers:

Answer

The newest release of angularJs (1.5.7) includes some email address validation improvements.

You can check the commit here.

Line 28 corresponds to the committed regex:

var EMAIL_REGEXP = /^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+\/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+\/0-9=?A-Z^_`a-z{|}~]+)*@[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])?)*$/;

Below the directive's code you have the tests to validate the regex. It does check for white spaces.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.