0

I created a custom input control and trying to perform certain validation on blur. But its not performing as expected. I want to use template like below instead of using jquery specific element.bind('blur')

 template: '<input type="text" ng-blur="performvalidation()">',

Complete fiddle here

Please guide or correct what am I doing wrong. Thanks.

1
  • Please add more code . So we can understand what you are trying to do . Commented May 7, 2016 at 19:04

1 Answer 1

1

If you want to create custom validators you should add them to the ngModelController's $validators field. e.g.

angular.module('app').directive('strongSecret', function() {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, element, attr, ctrl) {
      ctrl.$validators.uppercaseValidator = function(value) {
        return /[A-Z]/.test(value);
      }

      ctrl.$validators.numberValidator = function(value) {
        return /[0-9]/.test(value);
      }

      ctrl.$validators.sixCharactersValidator = function(value) {
        return value.length === 6;
      }
    }
  };
});

Also instead of giving your directive a template you should just use it on an input element

<input ng-model="strongSecret" strong-secret name="strongSecret"/>

if you don't want to show the errors until the user clicks away from the input field you could do this

<ul ng-if="sampleForm.strongSecret.$touched" class="error-msgs" ng-messages="sampleForm.strongSecret.$error">
...
</ul>

Working jsFiddle: https://jsfiddle.net/e81kee9z/2/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.