- Angular JS Tutorial
- AngularJS Home
- AngularJS Introduction
- AngularJS Expressions
- AngularJS Directives
- AngularJS Modules
- AngularJS Controllers
- AngularJS Scopes
- AngularJS Data Binding
- AngularJS Services
- AngularJS HTTP
- AngularJS Filters
- AngularJS Forms
- AngularJS Validation
- AngularJS DOM Manipulation
- AngularJS Select
- AngularJS Tables
- AngularJS Events
- AngularJS Routing
- AngularJS Includes
- AngularJS Animations
- AngularJS Dependency Injection
- AngularJS API
- AngularJS Deployment
AngularJS Forms
AngularJS forms are an integral part of web applications for collecting user input. They provide features for validation, tracking user interactions, and submitting data seamlessly.
Key Features of AngularJS Forms
- Two-Way Data Binding: Synchronizes form inputs with the model in real time.
- Validation: Built-in validators for common requirements, such as required fields and email format.
- State Tracking: Tracks states like
dirty
,touched
, andvalid
. - Custom Validation: Allows creating custom validators for specific needs.
- Error Handling: Provides dynamic feedback based on validation status.
Common Form States
- $pristine: The form has not been interacted with.
- $dirty: The form has been modified.
- $touched: The field has been focused and blurred.
- $untouched: The field has not been focused.
- $valid: All validations have passed.
- $invalid: One or more validations have failed.
Creating Forms in AngularJS
Example: Basic Form
<form name="myForm" ng-submit="submitForm()">
<label>Name:</label>
<input type="text" name="userName" ng-model="user.name" required>
<span ng-show="myForm.userName.$error.required">Name is required.</span>
<br>
<label>Email:</label>
<input type="email" name="userEmail" ng-model="user.email" required>
<span ng-show="myForm.userEmail.$error.required">Email is required.</span>
<span ng-show="myForm.userEmail.$error.email">Invalid email format.</span>
<br>
<button type="submit" ng-disabled="myForm.$invalid">Submit</button>
</form>
Form Validation in AngularJS
Built-In Validators
- required: Ensures a field is not empty.
- email: Validates the format of an email address.
- number: Validates numeric input.
- minlength / maxlength: Ensures input length is within bounds.
- pattern: Validates input against a regular expression.
Example: Validation
<input type="text" name="password" ng-model="user.password" minlength="6" required>
<span ng-show="myForm.password.$error.required">Password is required.</span>
<span ng-show="myForm.password.$error.minlength">Password must be at least 6 characters.</span>
Custom Validators
You can define custom validators using AngularJS directives.
Example: Custom Validator for Age
app.directive('ageValidator', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
ngModel.$validators.age = function(value) {
return value >= 18;
};
}
};
});
Usage:
<input type="number" name="age" ng-model="user.age" age-validator required>
<span ng-show="myForm.age.$error.age">You must be at least 18 years old.</span>
Submitting Forms
AngularJS allows you to handle form submissions using the ng-submit
directive.
Example: Form Submission
<form name="myForm" ng-submit="submitForm()">
<input type="text" name="username" ng-model="user.name" required>
<button type="submit" ng-disabled="myForm.$invalid">Submit</button>
</form>
<script>
app.controller('FormController', function($scope) {
$scope.submitForm = function() {
if ($scope.myForm.$valid) {
alert('Form submitted successfully!');
}
};
});
</script>
Summary
AngularJS forms simplify user input handling by offering real-time validation, error tracking, and submission support. With features like state tracking, custom validators, and built-in validation, AngularJS forms enhance both user experience and developer productivity.