Check for Integer Only

snow_04
Giga Contributor

Hi,

We have a requirement to check that the value entered in a Text Box is a Integer only i.e. No characters should be allowed to be entered.

This is what I have tried so far:

HTML:

<input type = "textbox" id="test" ng-model="c.test" ng-change="c.checkint()">

 

Client Controller:

c.checkint = function()

{

var num = c.test;

var reg = /^\d+$/;

if(!reg.test(num))

{

alert('Enter Integer only');

}

}

 

Issue is even if numbers are entered it gives me the alert message i.e. allowing both Characters and Integers to be entered. Is there a way we can do this in Service portal widget i.e. allowing only numbers to be entered and if character is entered should throw an alert and clear the input text.

 

7 REPLIES 7

the only problem with isNaN is that it will allow any numerics, not just integers. so 123.5 would also pass the isNaN check.

That's a good point Jon, if he only want's integers your response is definitely the way to go.

Swathi12
Tera Expert

In client script try this:

 

 if ( !isNaN(test) && angular.isNumber(+test)) {

do something } else {
alert("Not an integer");
c.test ='';
}

Please do let me know if it was useful.