In this article I will explain with an example, how to perform Email Address validation inside Controller in AngularJS.
This article will illustrate how to use AngularJS Form Validation for TextBox and ng-pattern directive and Regular Expressions (Regex) for validating Email Address in AngularJS.
The Regular Expression for Email validation will be set inside Controller in AngularJS.
 
 
Validating Email using ng-pattern and Regular Expressions using AngularJS
The below HTML Markup consists of an HTML DIV to which ng-app and ng-controller AngularJS directives have been assigned.
Note: If you want to learn more about these directives, please refer my article Introduction to AngularJS.
 
The AngularJS app HTML DIV consists of an HTML Form with a TextBox to be validated, two HTML SPAN elements for displaying the error message for Required and Email validations and a HTML Button.
Controller
Inside the Controller, the Regular Expression (Regex) is set to a Scope variable and later the scope variable will be assigned to the ng-pattern directive.
Form
ng-submit< - The HTML Form has been assigned ng-submit AngularJS directive which has the expression MyForm.$valid which means the Form will be submitted only if the $valid property of the Form is TRUE.
novalidate – It is an HTML5 attribute which directs the Browser to disable the HTML5 validations for the particular Form.
 
TextBox (INPUT)
ng-pattern – The ng-pattern AngularJS directive is used to specify the Regular Expressions (Regex) pattern for validating the TextBox text.
required – It is an HTML5 attribute which is used to specify that the TextBox cannot be kept empty during Form submission.
 
SPAN
ng-show – The ng-show AngularJS directive will make the HTML SPAN visible only when the expression to be tested returns TRUE.
The first HTML SPAN will be visible when the Required validation for the Email TextBox is failing while the second HTML SPAN will be visible when the Regular Expressions (Regex) pattern validation for the Email TextBox is failing.
 
Submit Button
ng-disabled – The ng-disabled AngularJS directive will disable the Submit Button and enable only when the expression to be tested returns TRUE.
$invalid – The Submit Button will be enabled only if the $invalid property of the Form is FALSE.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
    var app = angular.module('MyApp', [])
    app.controller('MyController', function ($scope) {
        $scope.Regex = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    });
</script>
<div ng-app="MyApp" ng-controller="MyController">
    <form name="MyForm" ng-submit="MyForm.$valid" novalidate>
    <table border="0" cellpadding="0" cellspacing="5">
        <tr>
            <td>
                Email:
            </td>
            <td>
                <input type="text" ng-model="EmailAddress" name="Email" required ng-pattern="Regex" />
            </td>
            <td>
                <span class="error" ng-show="MyForm.Email.$error.required">*Required</span> <span
                    class="error" ng-show="MyForm.Email.$error.pattern">*Invalid Email Address</span>
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <hr />
            </td>
        </tr>
        <tr>
            <td colspan="2" align="right">
                <button type="submit" ng-disabled="MyForm.$invalid">
                    Submit</button>
            </td>
        </tr>
    </table>
    </form>
</div>
 
 
Screenshot
Validate Email inside Controller using Regular Expressions in AngularJS
 
 
Browser Compatibility

The above code has been tested in the following browsers only in versions that support HTML5.

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.

 
 
Demo
 
 
Downloads