Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Angular input fields email validation inside controller?

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.

like image 277
Alexandru R Avatar asked Mar 26 '14 12:03

Alexandru R


2 Answers

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

like image 144
kazuar Avatar answered Sep 21 '22 10:09

kazuar


You could use form and prevent form submission by adding ng-disabled to the submit button:

<form name="form">
      <input type="email" ng-model="email" name="email" required/>
      <button type="submit"
        ng-disabled="form.$invalid">Submit</button>
</form>

{{form.email.$valid}}

As the point of validation is to prevent submission and show error messages. I think it's enough to do it there instead of controllers where you handle your business logic.

DEMO

like image 29
Khanh TO Avatar answered Sep 19 '22 10:09

Khanh TO