How to add custom validation to an AngularJS form?


I have a form with input fields and validation setup by adding the required attributes and such. But for some fields I need to do some extra validation. How would I “tap in” to the validation that FormController controls?

Custom validation could be something like “if these 3 fields are filled in, then this field is required and needs to be formatted in a particular way”.

There’s a method in FormController.$setValidity but that doesn’t look like a public API so I rather not use it. Creating a custom directive and using NgModelController looks like another option, but would basically require me to create a directive for each custom validation rule, which I do not want.

Actually, marking a field from the controller as invalid (while also keeping FormController in sync) might be the thing that I need in the simplest scenario to get the job done, but I don’t know how to do that.


Edit: added information about ngMessages (>= 1.3.X) below.

Standard form validation messages (1.0.X and above)

Since this is one of the top results if you Google “Angular Form Validation”, currently, I want to add another answer to this for anyone coming in from there.

There’s a method in FormController.$setValidity but that doesn’t look like a public API so I rather not use it.

It’s “public”, no worries. Use it. That’s what it’s for. If it weren’t meant to be used, the Angular devs would have privatized it in a closure.

To do custom validation, if you don’t want to use Angular-UI as the other answer suggested, you can simply roll your own validation directive.

app.directive('blacklist', function (){ 
   return {
      require: 'ngModel',
      link: function(scope, elem, attr, ngModel) {
          var blacklist = attr.blacklist.split(',');

          //For DOM -> model validation
          ngModel.$parsers.unshift(function(value) {
             var valid = blacklist.indexOf(value) === -1;
             ngModel.$setValidity('blacklist', valid);
             return valid ? value : undefined;

          //For model -> DOM validation
          ngModel.$formatters.unshift(function(value) {
             ngModel.$setValidity('blacklist', blacklist.indexOf(value) === -1);
             return value;

And here’s some example usage:

<form name="myForm" ng-submit="doSomething()">
   <input type="text" name="fruitName" ng-model="data.fruitName" blacklist="coconuts,bananas,pears" required/>
   <span ng-show="myForm.fruitName.$error.blacklist">
      The phrase "{{data.fruitName}}" is blacklisted</span>
   <span ng-show="myForm.fruitName.$error.required">required</span>
   <button type="submit" ng-disabled="myForm.$invalid">Submit</button>

Note: in 1.2.X it’s probably preferrable to substitute ng-if for ng-show above

Here is an obligatory plunker link

Also, I’ve written a few blog entries about just this subject that goes into a little more detail:

Angular Form Validation

Custom Validation Directives

Edit: using ngMessages in 1.3.X

You can now use the ngMessages module instead of ngShow to show your error messages. It will actually work with anything, it doesn’t have to be an error message, but here’s the basics:

  1. Include <script src="angular-messages.js"></script>
  2. Reference ngMessages in your module declaration:

    var app = angular.module('myApp', ['ngMessages']);
  3. Add the appropriate markup:

    <form name="personForm">
      <input type="email" name="email" ng-model="" required/>
      <div ng-messages="$error">
        <div ng-message="required">required</div>
        <div ng-message="email">invalid email</div>

In the above markup, ng-message="$error" basically specifies a context for the ng-message child directives. Then ng-message="required" and ng-message="email" specify properties on that context to watch. Most importantly, they also specify an order to check them in. The first one it finds in the list that is “truthy” wins, and it will show that message and none of the others.

And a plunker for the ngMessages example

Answered By – Ben Lesh

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.