1

I am trying validate the textbox allow only integer number and greater than zero values.

Here I attached my code.

Html:

<body ng-app="myApp">
  NUMBER ONLY <input type="text" allow-pattern="\d" />

</body>

Js:

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

app.directive('allowPattern', [allowPatternDirective]);

function allowPatternDirective() {
    return {
        restrict: "A",
        compile: function(tElement, tAttrs) {
            return function(scope, element, attrs) {
                element.bind("keypress", function(event) {
                    var keyCode = event.which || event.keyCode; // I safely get the 
                    if (!keyCodeChar.match(new RegExp(attrs.allowPattern, "i"))) {
            event.preventDefault();
                        return false;
                    }          
                });
            };
        }
    };
}

Also I tried like this bellow:

<body ng-app="myApp">
  NUMBER ONLY <input type="text" allow-pattern="^[1-9][0-9]*$" />

</body>

But its not working.

Check jsfiddle link: click here

24
  • why a separate directive? you may use ng-pattern Commented Mar 31, 2016 at 9:22
  • Possible duplicate of angular validate input type="number" Commented Mar 31, 2016 at 9:24
  • @Gianmarco - input type="number" is not suitable for my case, because the textbox text or number option based on another dropdown select option. Its possible to do in my example? Commented Mar 31, 2016 at 9:27
  • did you tried to print keyCodeChar var? Commented Mar 31, 2016 at 9:27
  • @AvinashRaj - ng-pattern is validate after form submit, I expect allow only greater than 0 values. Commented Mar 31, 2016 at 9:28

1 Answer 1

0

You can make use of angular form validation and also use ng-model-options

Here is the link to Codepen

Controller snippet :

var app = angular.module('app',[]);
app.controller('myCtrl',function($scope){
 $scope.onlyNumbers = /^[1-9]+[0-9]*$/;
})

View :

<div ng-app="app">
<br />
<br />
<div class="col-md-2" ng-controller="myCtrl">
  <form name="myForm1">
  <div class="form-group" ng-class="{'has-error':myForm1.number1.$invalid}">
    <label for="">Following validation happens as the user entrs a value.</label>
    <input class="form-control" ng-pattern="onlyNumbers" ng-model="value1" name="number1"/> Valid? {{myForm1.number1.$valid}}
  </div>
</form>
<form name="myForm2">
  <div class="form-group" ng-class="{'has-error':myForm2.number2.$invalid}">
    <label for="">Following validation happens after blur </label>
    <input class="form-control" ng-pattern="onlyNumbers" ng-model="value2" name="number2" ng-model-options="{ updateOn: 'blur'}"/> Valid? {{myForm2.number2.$valid}}
  </div>
</form>

For more on how you can better this process through controllers refer this link

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.