Check for Integer Only
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2018 07:20 AM
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.
- Labels:
-
Service Portal Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2018 11:34 AM
the only problem with isNaN is that it will allow any numerics, not just integers. so 123.5 would also pass the isNaN check.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2018 12:22 PM
That's a good point Jon, if he only want's integers your response is definitely the way to go.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2018 04:57 AM
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.