AngularJS can validate input data..
AngularJS offers client-side form validation.
AngularJS monitors the state of the form and its input fields (like <input>
, <textarea>
, <select>
), and lets you notify the user about their current state.
It also tracks whether the fields have been touched, modified, or left untouched.
You can use standard HTML5 attributes like required
, minlength
, or write your own validation functions.
Use the HTML5 attribute required
to specify that the input field must be filled out.
Use the HTML5 type E-mail to specify that the value must be an e-mail:
AngularJS constantly updates the state of the form and the input fields.
$untouched
– Field not touched yet$touched
– Field has been touched$pristine
– Field not modified$dirty
– Field has been modified$invalid
– Invalid content$valid
– Valid content$pristine
– No fields modified$dirty
– One or more modified$invalid
– Form is invalid$valid
– Form is valid$submitted
– Form has been submittedUse these states to show helpful messages. For example, warn the user if a required field is left blank.
AngularJS automatically adds or removes CSS classes to input fields and forms based on their validation state.
ng-untouched
– Not yet touchedng-touched
– Has been touchedng-pristine
– Not modifiedng-dirty
– Modifiedng-valid
– Content is validng-invalid
– Content is invalidng-valid-required
, ng-invalid-required
– Specific to `required` validationng-pristine
– No fields modifiedng-dirty
– One or more fields modifiedng-valid
– Form is validng-invalid
– Form is invalid✅ These classes help create dynamic styles and give users feedback (e.g., red border for invalid input).
Add styles for these classes to give your application a better and more intuitive user interface.
These classes let you style your form fields and entire forms based on their validation state.
AngularJS applies ng-pristine
and ng-dirty
classes to the <form>,
and styles it accordingly (lightblue when untouched,
pink when modified). ✅
To create your own validation function is a bit more tricky. You need to:
✅ Create a new directive in your AngularJS application
✅ Handle the validation logic inside a function using specific arguments
✅ Attach your directive to an input field
In the following example, we create a directive called my-directive
.
The input field will only be valid if the value contains the character "e".
Below is a complete example demonstrating this validation in action.
In HTML, the new directive will be referred to by using the attribute my-directive
.
In the JavaScript, we start by adding a new directive named myDirective
.
Remember, when naming a directive, you must use a camelCase name like myDirective
,
but when using it in HTML, you must refer to it with kebab-case: my-directive
.
Then, return an object where you specify that it requires ngModel
,
which refers to the ngModelController
.
Next, create a linking function that takes several arguments. The fourth argument,
often called mCtrl
, is the actual model controller.
Inside that link function, define your custom validation function — for example, one that checks if
the input value contains the letter "e". Based on the check, you use
mCtrl.$setValidity()
to mark the input as valid or invalid.
Finally, use mCtrl.$parsers.push(myValidation)
to add your validation function to AngularJS’s
input parsing pipeline. This ensures the validation runs every time the input value changes.
The AngularJS directive ng-model
binds the input elements to the model.
In this case, the model object has two properties: user
and email
.