AngularJS bind ng-valid with function

267 Views Asked by At

Hi i would like to setValidity of input using an expression, but not a regEx, something like this

<input ng-model="number" required ng-valid="number / 2 > 14">

Is it possible i have complex function and i don't know how to convert it into regEx so i would like to use something like this. I try to use this library: https://github.com/turinggroup/angular-validator but it doesn't work at all :/

1

There are 1 best solutions below

0
Sudhir Ojha On

You may try something like this:

<div ng-app="myApp">
    <div ng-controller="myController">
        <input ng-model="number" required ng-keyup="checkValid(number)">
        <span style="color:red" ng-if="!isValid">Not a valid number</span>
    </div>
</div>

And create a function like following:

(function(){

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

    myApp.controller('myController', function($scope, $timeout){
        $scope.isValid = true;

      $scope.checkValid = function(value){
        if(value){
            if(value/2>14){
            $scope.isValid = true;
          }else{
            $scope.isValid = false;
          }
        }
      }  


    });

})();