In this article I will explain with an example, how to perform Form validation in AngularJS.
This article will illustrate how to perform Form Validation for TextBox INPUTS for validating Name, Email Address and Age, etc. on click of Submit Button in AngularJS.
 
 
Validating Form on Submit Button Click in 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 HTML TextBoxes (Text, Email and Number) to be validated, HTML SPAN elements for displaying the error message for Required, Email and Number validations and a HTML Button.
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)
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.
$dirty – This Flag is initially FALSE and is TRUE when User has modified the content in the field.
The first HTML SPAN will be visible when the Required validation for the TextBox is failing while the second HTML SPAN will be visible when the Email or Number validation for the Email TextBox and Number TextBoxes are 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) {
    });
</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>Name:</td>
            <td><input type="text" ng-model="Name" name="Name" required /></td>
            <td>
                <span class="error" ng-show="MyForm.Name.$dirty && MyForm.Name.$error.required">*Required</span>
            </td>
        </tr>
        <tr>
            <td>Email:</td>
            <td><input type="email" ng-model="EmailAddress" name="Email" required /></td>
            <td>
                <span class="error" ng-show="MyForm.Email.$dirty && MyForm.Email.$error.required">*Required</span>
                <span class="error" ng-show="!MyForm.Email.$error.required && MyForm.Email.$invalid">*Invalid Email Address</span>
            </td>
        </tr>
        <tr>
            <td>Age:</td>
            <td><input type="number" ng-model="Age" name="Age" required /></td>
            <td>
                <span class="error" ng-show="MyForm.Age.$dirty && MyForm.Age.$error.required">*Required</span>
                <span class="error" ng-show="!MyForm.Age.$error.required && MyForm.Age.$invalid">*Only numerical value</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
Form Validation AngularJS: Validate Form on Submit Button Click 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