1

I have a problem with e-mail validation. I use a Angular.js. In my form I have a input:

<input class="form-control input-flat-underline" ng-pattern="/^[\w\.-]+@[\w\.-]+\.[\w]+$/" id="email" name="email" ng-model="ctrl.user.email" required type="email"> 

The e-mail

[email protected]

is not ok but validation passed, But e-mail is not correct. I think the problem is in ng-pattern.

3
  • Becaus [email protected] is not valid email address. Commented May 24, 2016 at 8:10
  • yes, but validation passed Commented May 24, 2016 at 8:13
  • You don't need ng-pattern when you have type="email", it contains the email validation. Commented May 24, 2016 at 9:08

2 Answers 2

2

I assume you want to validate email address. So in this case you don't need to create your own regular expression pattern, because the proper one is going to be quite complex. You don't want to do this.

And this is the reason, why you had this problem: even though "[email protected]" passess ngPattern validation (but it should not, this is not valid email address, and your regular expression is not standard compliant), it fails on type="email" validation (Angular one).

Angular has build-in email validation regular expression (this one here) which you can make use of simply by using input type="email". So in your case to make correct email address validation all you need to do is to remove ngPattern:

<input class="form-control input-flat-underline" id="email" name="email" ng-model="ctrl.user.email" required type="email"> 
Sign up to request clarification or add additional context in comments.

1 Comment

Use ng-message="email" instead of ng-message="pattern".
0

You can try the angular built-in email validation by removing the ng-pattern option from the input tag. Like the following:

<input type="email" name="input" ng-model="email.text" required> 

Otherwise you can try the validation with the following:

<input type="email" name="input" ng-model="email.text" ng-pattern="/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/" required> 

Comments