How to use Angular input fields email validation inside controller?

Alexandru R picture Alexandru R · Mar 26, 2014 · Viewed 8.4k times · Source

I'm trying to validate a variable as email inside Angular controller.

var valid = $filter('email')($scope.email);

The email filter doesn't exists, generates "unknown provider error". What's the correct way to access email validation from inside the controller?

Thanks.

Later edit: don't put me to create a custom filter, it must be a way using angular validation.

Answer

kazuar picture kazuar · Dec 19, 2014

You can create a hidden input type email:

<input type="email" hidden id="emailValidator" required ng-model="x" />

Then in js set the value to that input and check validity:

var validator = $('#emailValidator')[0];
validator.value = "[email protected]";

return validator.checkValidity();

Also this documentation could be helpful: https://docs.angularjs.org/guide/forms