1
function isNumber(n) {
    var j = n.trim();
  return (j % 1 === 0 && j != "");
}

My function still returns true if the inputted value is "14.0". It should only allow whole numbers without any decimal.

mengmeng
  • 1,266
  • 2
  • 22
  • 46
  • If in ES6 environment, use `Number.isInteger`. If IE>=10, use `pattern="\d+"` attribute on the input element. –  Mar 16 '15 at 03:58

3 Answers3

5

You can use regex for this

function isWhole(n) {
  return /^\d+$/.test(n);
}

$("#number").change(function() {
  if (isWhole($(this).val())) {
    $(".error").hide();
  } else {
    $(".error").show();
  }
});
.error {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="number" name="number" />
<p class="error">Whole numbers only</p>
Prathik Rajendran M
  • 1,152
  • 8
  • 21
  • This doesn't work per OP's requirement. `var moon = 1234.0` returns true. – Millie Smith Mar 16 '15 at 02:19
  • Hmm.. The regex only matches numbers and no other character including . but I guess that applies to strings only. – Prathik Rajendran M Mar 16 '15 at 02:28
  • The one and only, memory-stealing Google Chrome. The problem is that after `var moon = 1234.0; var str = moon.toString()`, `str` is equal to `"1234"`. – Millie Smith Mar 16 '15 at 02:30
  • I guess 1234.0 is treated as 1234 if it is a type "number". But since OP is calling trim on the variable n, I think we can assume that it is a string. – Prathik Rajendran M Mar 16 '15 at 02:35
  • I am always staggered by how much jQuery someone can fit in an answer that isn't even about jQuery. Especially when it's several times more code than the actual answer. – RobG Mar 16 '15 at 04:52
  • @PrathikRajendranM is right. It;s treated as a string. – mengmeng Mar 16 '15 at 05:07
  • @RobG that was just to demonstrate it working, the function is not in jQuery. However I get your point. How to add 2+2 in javascript? var v = 2 + 2; Downvoted cos not enough jquery. – Prathik Rajendran M Mar 16 '15 at 06:35
  • 1
    @RobG Also Rob, I don't think using jQuery to demonstrate a functionality is wrong. It is concise and it is good to show the working of a code since they could directly copy it from here and know it works as per their need. I don't agree with this anti-jQuery brigade that irrationally targets anything that has jQuery in it. – Prathik Rajendran M Mar 16 '15 at 08:08
1

Try treating it as a string and use the match function with regex to see if there is a decimal there. You can use yourVariable.match (/[0-9]*(\.)[0-9]*/). It will return true if there is a decimal there.

Moishe Lipsker
  • 2,974
  • 2
  • 21
  • 29
0

Abuse the round() function. That will get rid of the garbage:

function isNumber(n) {
  if(Math.round(n)==n && n.length==(Math.round(n)).length) 
    return n;
  else return false;
}

Essentially this function returns false if the number is not an integer, and returns the exact integer if it is an integer.

Input 14.0 should return false

Input 13.5 should return false

Input 13 should return 13.

Norbert
  • 6,026
  • 3
  • 17
  • 40