1

I am new to angular js and want to validate a field on blur. As i am entering the value the directive is called and is validating the entered value. i want this to happen on -blur. I have already user on-blur in the html for calling some other function.

Below is the directive :

app.directive("emailCheck", function(){
return {
    require: 'ngModel',
    link: function (scope, elm, attrs, ctrl) {
        ctrl.$parsers.unshift(function (viewValue, $scope) {
            var emailPattern =  /\S+@\S+\.\S+/;
            var isEmailValid = emailPattern .test(viewValue)
            ctrl.$setValidity('emailFormat', isEmailValid);
            return viewValue;
        })
    }
 };
});

How do i check for on blur event here?

Thanks in advance.

4 Answers 4

2

you should bind blur event to directive. try like this:

link: function (scope, elm, attrs, ctrl) {
    elm.bind('blur',function(){
   }) 
}

var app = angular
  .module('MyApp', [
  ])
.controller('Main', ['$scope', function ($scope) { 

}])
.directive("emailCheck", function(){
return {
    require: 'ngModel',
    link: function (scope, elm, attrs, ctrl) {
      elm.bind('blur',function(){
        alert("test");
      }) 
    }
 };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
 <div class="main-content" ng-app="MyApp" ng-controller="Main as myCtrl">
      <div>
           <input type="text" ng-model="email" email-check>
     </div>
</div>

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

Comments

2

Conventionally, when you use ngModel in your directive, you do not decide what event to hook into. You subscribe to whatever event the ngModel is hooked up to. This is not just blur by default.

The user of your directive can use ngModelOptions with updateOn:blur`` to update only on blur, like:

<input 
    email-check
    name="testEmail" ng-model="testEmail"
    ng-model-options="{ updateOn: 'blur' }">

<!-- Note using "updateOn: 'blur'" NOT "updateOn: 'default blur'" -->

ngModelOptions Docs

Comments

1

You will have to handle blur event inside the directive and remove the same from html element to handle it in the below fashion:

app.directive("emailCheck", function(){
return {
    require: 'ngModel',
    link: function (scope, elm, attrs, ctrl) {
        elm.on('blur', function() {
           // @TODO you will have to put your code here to handle this event
        });
    }
 };
});

Comments

0

Try this.

define(['app'],function(app){
app.directive('myDecimals', function() {
   return {
     restrict: 'A',
     link: function(scope, elm, attrs) {

       elm.bind('blur',function(){
           elm.val(elm.val()*1);
       });
     }
   };
 });
 });

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.