1

So to check if a string is a positive integer, I have done some research and found this solution here by someone: Validate that a string is a positive integer

function isNormalInteger(str) {
    return /^\+?(0|[1-9]\d*)$/.test(str);
}

However, I put this into test, and found that numbers with pure 0's on the decimal places does not seem to be working. For example:

15 ===> Works!

15.0 ====> Does not work :(

15.000 ===> Does not work :(

Build upon the existing method, how could I allow pure-0's on the decimal places and make them all work? Please note 15.38 should not work, but 15.00 should.

Community
  • 1
  • 1
photonacl
  • 135
  • 1
  • 3
  • 14
  • Integers don't have decimal places though, `15.0` isn't an integer, that's why it doesn't work when *"put to the test"*. – Spencer Wieczorek Jul 22 '15 at 21:37
  • Yes preferably. I would like to not have leading "+" sign, and allow pure 0's in the decimal places. My best solution so far is ^[0-9]\d*(\.0+)*$, does this seem to be correct? Thanks. @anubhava – photonacl Jul 22 '15 at 22:13
  • Did you check my answer below? Isn't it already matching all of your conditions? – anubhava Jul 22 '15 at 22:14

3 Answers3

3

No need to use regex here.

function isNormalInteger(str) {
   var n = parseInt(str);
   return n > 0 && n == +str;
}

Then test it:

isNormalInteger(15)
true
isNormalInteger(15.00)
true
isNormalInteger(15.38)
false
isNormalInteger(-15)
false
isNormalInteger(-15.1)
false
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Actually this solution, may rise an Error because `parseInt()` will give `NaN` if the given string is not a valid number. – cнŝdk Jul 22 '15 at 22:42
  • Nope, there is no error. If you call `isNormalInteger("abc")` it will **correctly return `false`** – anubhava Jul 23 '15 at 04:14
1

Quick and dirty

function isNormalInteger(str) {
    var ival=parseInt(str);
    return ival!=NaN && ival>=0 && ival==parseFloat(str);
}
Bob Stiles
  • 11
  • 3
1

First of all the function should be called isNormalNumber instead of isNormalInteger as it accepts decimals, then this is the REgex you need:

    function isNormalNumber(str) {
      return /^\+*[0-9]\d*(\.0+)?$/.test(str);
    }

    alert(isNormalNumber("+10.0") + "::" + isNormalNumber("+10.9") + "::" + isNormalNumber("10"));

Returns true::false:true.

EDIT:

This is an edit to avoid matching leading zeros like in the numbers 001234 and 07878:

^\+*[1-9]\d*(\.0+)?$|^0(\.0+)?$
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • Hi chsdk, the regex is great, but how could I allow 0 or 0.00 etc as well? I tried /^\+*[0-9]\d*(\.0+)*$/ is this correct? is [0-9] a valid range? @chsdk – photonacl Jul 22 '15 at 21:53
  • Yep worked like a charm, and to eliminate the option of a leading "+", I just removed \+* correct? @chsdk – photonacl Jul 22 '15 at 22:09
  • 1
    It matches `0` which is not a positive integer and it also matches `0.0.0.0` which is not even a number. – anubhava Jul 22 '15 at 22:17
  • Great catch! Thanks! Actually 0 is fine, for 0.0.0.0, I used ^[0-9]\d*(\.0+)?$ and now it seem to be working fine, I changed * to ? , is this a correct approach? @anubhava – photonacl Jul 22 '15 at 22:24
  • Correct approach is using **without any regex** as I showed below but for regex `/^\d+(?:\.0+)?$/` is fine for you. – anubhava Jul 22 '15 at 22:27
  • Actually, the solution that you suggested may rise an Exception and give `NaN` if the given str can't be parsed as Integer, for example `parseInt("Hello")` wil give `NaN`. – cнŝdk Jul 22 '15 at 22:41
  • Not correct, there is no error. If you call `isNormalInteger("abc")` then **it will correctly return false**. Have you tested it? – anubhava Jul 23 '15 at 04:16
  • Actually I found a situation this does not solve, that is, when I enter 001234, it will also accepts it. Anyway to remove the leading 0's ? @chsdk – photonacl Jul 28 '15 at 00:03
  • @photonacl take a look at my Edit. – cнŝdk Jul 28 '15 at 07:47
  • @chsdk thanks, 0.0.0 still got accepted so I change the last * to ?, but after the change, 0.000 still not working where it supposed to be... – photonacl Jul 28 '15 at 17:47
  • @chsdk, could you take a look if this is correct? ^(0|[1-9]\d*)(\.0+)?$ , i tested it couple of times and it seemed to be working nicely. – photonacl Jul 28 '15 at 18:08
  • @photonacl This is what you need : `(\.0+)?` take a look at the one in my edit. – cнŝdk Jul 28 '15 at 20:22