Validate email with angularjs

0

I have the following field for email:

<input type="email" class="form-control" id="email" name="email" ng-model="fields.email" required="true" />

In the div where the field is, I have the following validation:

ng-class="{ 'has-error': userForm.email.$error.email }"

It turns out that typing "abc @ abc" is given as valid, but as we can see it is wrong.

I have tried to use regular expression but without success. How to validate emails correctly in AngularJS?

    
asked by anonymous 19.09.2014 / 00:11

2 answers

1

Use ngPattern to change the default behavior of Angular:

<input type="email" <!-- ... --> ng-model="fields.email" ng-pattern="/^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@((\[[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,}))$/" />

Check out the Angular documentation for more information:

AngularJS: input [email]

    
19.09.2014 / 00:29
2

In fact, the abc @ abc email can be valid, just like email @ localhost.

It's unusual, but it's technologically valid, it could respond to some address.

You can check in: link

I do not even know how this will behave with new custom domains like .google or .globo, they already exist and may be able to use emails in this way, but then I'm not sure.

    
19.09.2014 / 00:34