0

I have the next problem, I created a directive in Angular JS to check if input is or not a number, If it isn't number is neccesary set the right value. Here is my directive code:

app.directive('numberValidate', function(){
    return {
        require: 'ngModel',
        link: function($scope, element, attr, modelCtrl){

            modelCtrl.$parsers.push(function (inputValue){

                var transformedInput = parseInt(inputValue); 

                if (transformedInput!=inputValue) {
                   modelCtrl.$setViewValue(transformedInput);
                   modelCtrl.$render();
                }
                return transformedInput;                
            });
        }
    };
});

additional I need to listen when the user stop typping to send a request. To listen when stop I've added ng-model-options="{debounce: 750}" to the field, as it is on the next code:

<input type="text" ng-change="sendChecker()" ng-model="identification" ng-model-options="{debounce: 750}" number-validate>

sendChecker function fires my request to the server, everything is working Ok when I write a number like 12345 or I type a number with characteres 12345a this is converted to number and set in the input, The issue is when I write a letter a this creates a loop. How can I avoid this?

Any advice would be apreciated.

4
  • Shouldn't you be checking that transformedInput is actually a number? Commented May 4, 2016 at 20:11
  • 1
    Try the isNaN() function at: w3schools.com/jsref/jsref_isNaN.asp Commented May 4, 2016 at 20:15
  • Maybe just use input type number? Shortcut, but it only accepts numbers. Then work the rest out. Commented May 4, 2016 at 20:26
  • @alphapilgrim If I detect is not number, I have set the right on the input text in this case 12345a would be 12345. Commented May 4, 2016 at 20:29

2 Answers 2

2

Why you don't use ng-pattern to validate your input?

<input type="text" ng-pattern=/^[0-9]+$/ ng-model="identification" ng-model-options="{debounce: 750}">

Or you can use input type number

 <input type="number" ng-model="identification" ng-model-options="{debounce: 750}">

Then you don't have to validate something by your own?

Edit:

In the following example sendChecker() is only called if user inserted a valid value

<form name="myform">
     <input type="number" name="myinput" ng-changed="myform.myinput.$valid ? sendChecker(identification) : " ng-model="identification" ng-model-options="{debounce: 750}">
</form>
Sign up to request clarification or add additional context in comments.

4 Comments

How would you recommend set the value in case ng-pattern? I mean if isn't integer I must set the number value
Sorry, I don't understand your question. Which value you want to set? To match numbers only you can use the pattern from my example.
Why you want change the content of input field?
Becuase the value of input on the request must be a number, and the value after the directive go to sendChecker method to be sent
0

Because parseInt('12345a') == 12345 you get into loop. So you need to either filter out the non digits from string.

1 Comment

When I write 12345a the behaviour is right, the problem is when I write a this is what create the loop.

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.