7

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.

3 Answers 3

7

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

Sign up to request clarification or add additional context in comments.

4 Comments

I'm not asking about form validation.
There is no form. The input field is a hack to solve this problem. I had exactly the same use case as you. I wanted to validate just a string and I used my solution to "hack" it that way instead of creating some cumbersome regex. Try it, it works.
I have a list of emails I need to evaluate from a textarea.
take your list of emails and loop through them and run the code I pasted on each of the email. Where's the problem?
4

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

5 Comments

doesn't answer the question. How to access filter validation in controller?
@Alexandru Rada: the approach of using filter to do form validation is not recommended. That's why I suggested another approach
What makes you think I'm trying to do form validation? I'm trying to evaluate a string as email. And everybody answer is related to input fields.
@Alexandru Rada: If you want to do validation for user inputs before sending to server, I think using this approach is recommended. If you want to do validation for other cases (like loaded data from server), maybe we can just create a utility service with regex
Exactly that utility I'm looking for. With the help of angular built-in email filter. Now you got it right.
3

You can write your own filter:

angular.module(/**some module description here**/).
   filter('email',function(){
       var validateEmail = function(email) { 
            var re = /^(([^<>()[\]\\.,;:\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,}))$/;
            return re.test(email);
       }
       return function(input){
            return validateEmail(input); 
       };
   });

Regular expression is borrowed from here

4 Comments

Why borrow from a SO question when you can just pick up the right expression from the source code ?
@svarog there are lots of ways for email's regexps. I have chosen the one from SO answer. Everybody is free to use their own or borrow from somewhere
you already get email regex validation out of the box (which you may already use in another part of your application), why add another and make implementation inconsistent ?
@svarog Yes, now it's native functionality, when I was writing my answer it was 2014 year :) . I am letting you to edit my answer with the following However if you are using new angularjs, then you can use bla bla bla .... Or you can creat a new answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.