angular.module('app').directive('validateConfirmField', validateConfirmField);
function validateConfirmField($parse)
{
return {
require: 'ngModel',
link: function (scope, elem, attrs, ctrl)
{
scope.$watch(function ()
{
if (!(ctrl.$modelValue) || !($parse(attrs.validateConfirmField)(scope)))
{
return true;
}
return ($parse(attrs.validateConfirmField)(scope)).toLowerCase() === ctrl.$modelValue.toLowerCase();
}, function (currentValue)
{
ctrl.$setValidity('validateConfirmField', currentValue);
});
}
};
}
<!-- HTML View part where you want to validate your fields -->
< input type = "text"
name = "name1"
value = "Field1"
ng - model = "field1" > < input type = "text"
name = "name2"
value = "Field2"
ng - model = "field2"
data - validate - confirm - field = "field1" >
<!-- HTML Validation -->
< span ng - show = "form.name2.$error.validateConfirmField" > Field1 and Field2 does not match. < /span>AngularJS Directive to check that passwords/emails match.
