Custom Validation
In angularJS we have power to create our own validation function . It is little bit tricky. We need to add a new directive to our application. And to deal with the validation in function we need to do a certain specified arguments.
In below example we have created our own directive and we name this is my-directive. Here we will check if input field has a character in it if it is there then we will get "True" else it is "False".
In Above example below are the most usefull points which helps you to understand the example:-
(1)In new directive will be referred to by using the attribute my-directive.
(2)Here in the JavaScript we start adding a new directive named as myDirective.
(3)Name should be choosen safely like camel case name myDirective. But when invoking this we must use "-" separated name as my-directive.
(4)When retrun object where we specify that we require ngModel which is the ngModelController.
(5)We Make a linking function which takes arguments and the fourth argument, mCtrl, is the ngModelController.
(6)We specify a function, and is named myValidation, which takes one argument, this argument is the value of the input element.
(7)Test if the value contains the letter "a", and set the validity of the model controller to either true or false.
(8)At last, mCtrl.$parsers.push(myValidation); will add the myValidation function to an array of other functions, which will be executed every time the input value changes. | |