future
future

Reputation: 352

Call a function on Valid Blur

PLUNKER

How can i trigger function call on blur only if element is valid has no other error?

HTML

<input type="text" id="order" ng-model="order" name="order" class="form-control" 
    ng-pattern="/^[0-9]*$/" ng-minlength="9" maxlength="9"
    required="" ng-blur="something()"  />

js

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
    $scope.something=function(){
      if($scope.routingNumber.$valid)
        alert("yippie");
    }
});

Upvotes: 4

Views: 12500

Answers (1)

apairet
apairet

Reputation: 3172

Here you go: http://plnkr.co/edit/g4ecmWToQPbOKAk3XvNt?p=preview (complete form) http://plnkr.co/edit/Zep8D4AXvkzwpqcFFXjR?p=preview (order field only)

You just have to pass your form to your something method.

<input type="text" id="order" ng-model="order" name="order" class="form-control" 
 ng-pattern="/^[0-9]*$/" ng-minlength="9" maxlength="9"
 required="" ng-blur="something(myform)"  />

And in your JS:

app.controller('MainCtrl', function($scope) {
  $scope.something = function(form) {
    if (form.order.$valid) {
      alert("yippie");
    }
  }
});

You can find more info about form validation and errors in the docs: https://docs.angularjs.org/guide/forms

Upvotes: 6

Related Questions