Skip to content

Instantly share code, notes, and snippets.

@neilbo
Created September 22, 2014 14:04
Show Gist options
  • Save neilbo/69ec1ba325044711228c to your computer and use it in GitHub Desktop.
Save neilbo/69ec1ba325044711228c to your computer and use it in GitHub Desktop.
Show an error when inputs match
.help-block {
display: none;
}
.has-error .help-block {
display: block;
}
<form name="myForm">
<input name="mobile1" ng-model="mobile.first" />
<input name="mobile2" ng-model="mobile.second" />
<p ng-if="myForm.mobile2.$error.mismatch && myForm.mobile2.$dirty" class="help-block">First Mobile and Second Mobile must NOT match.</p>
</form>
angular.module('myapp')
.directive('notmatch', function($parse) {
return {
require: 'ngModel',
link: function(scope, elem, attrs, ctrl) {
scope.$watch(function() {
return $parse(attrs.notmatch)(scope) === ctrl.$modelValue;
}, function(currentValue) {
ctrl.$setValidity('mismatch', !currentValue);
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment