I am facing a particular issue with my Angular JS directive that is designed to verify whether an input is a number or not. If the input is not a number, then it needs to be corrected. Here is the code for my directive:
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;
});
}
};
});
In addition, I also need to detect when the user stops typing in order to send a request. To achieve this, I have included
ng-model-options="{debounce: 750}"
in the field as shown in the following code:
<input type="text" ng-change="sendChecker()" ng-model="identification" ng-model-options="{debounce: 750}" number-validate>
The sendChecker
function triggers the request to the server. Everything works fine when I enter a number like 12345
or a combination of numbers and characters like 12345a
, as it gets converted to a number and displayed in the input field. However, the problem arises when I enter a letter a
, which creates a loop. How can I prevent this from happening?
I would greatly appreciate any advice on resolving this issue.