7

I am pulling a value via JavaScript from a textbox. If the textbox is empty, it returns NaN. I want to return an empty string if it's null, empty, etc.

What check do I do? if(NAN = tb.value)?

Teun Zengerink
  • 4,277
  • 5
  • 30
  • 32
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • Be more specific please: what's this textbox (a text input, textarea , select list or what)? No 'textbox' value (empty or not) returns NaN by itself to my knowledge. – KooiInc Apr 27 '09 at 13:36
  • Does the textbox return NaN or do you parse it to float/integer first (which would mean that parseFloat/Int) returns NaN. – Pim Jager Apr 27 '09 at 14:21
  • did you mean: if(NAN == tb.value) // (two equals for comparison) – Jay Aug 13 '09 at 23:41
  • Possible duplicate of [convert string to a number in javascript](http://stackoverflow.com/questions/11613705/convert-string-to-a-number-in-javascript) – Liam Jul 15 '16 at 14:25

4 Answers4

18

Hm, something is fishy here.

In what browser does an empty textbox return NaN? I've never seen that happen, and I cannot reproduce it.

The value of a text box is, in fact a string. An empty text box returns an empty string!

Oh, and to check if something is NaN, you should use:

if (isNaN(tb.value))
{
   ...
}

Note: The isNaN()-function returns true for anything that cannot be parsed as a number, except for empty strings. That means it's a good check for numeric input (much easier than regexes):

if (tb.value != "" && !isNaN(tb.value))
{
   // It's a number
   numValue = parseFloat(tb.value);
}
Tor Haugen
  • 19,509
  • 9
  • 45
  • 63
2

You can also do it this way:

var number = +input.value;
if (input.value === "" || number != number)
{
    // not a number
}

NaN is equal to nothing, not even itself.

if you don't like to use + to convert from String to Number, use the normal parseInt, but remember to always give a base

var number = parseInt(input.value, 10)

otherwise "08" becomes 0 because Javascript thinks it's an octal number.

fforw
  • 5,391
  • 1
  • 18
  • 17
1

Assuming you have a reference to the input text box:

function getInteger(input) {
  if(!input || !input.value) return "";

  var val = parseInt(input.value, 10);

  if(isNaN(val)) return "";
  else return val;
}
Ben Lowery
  • 344
  • 1
  • 4
0

One thing you could do is a regex check on the value of the textbox and make sure it fits the format of an accepted number, and then if it fits the format perform your process, otherwise return an empty string.

Edit: This is an example from some code I have in front of me (might not be the best regular expression):

var anum=/(^\d+$)/;

if (!anum.test(document.getElementById("<%=txtConceptOrderValue.ClientID %>").value))
{
    alert("Order Value must be a valid integer");
    document.getElementById("<%=txtConceptOrderValue.ClientID %>").focus();
    return false;
}

Edit 2: I should also note that I am using ASP.NET which is why I have the slightly funky way of accessing the textbox. In your regular JavaScript case it may not be as cluttered.

TheTXI
  • 37,429
  • 10
  • 86
  • 110
  • That's actually modified slightly for this answer. If you check the revision history you'll see that the original piece of code was to check for decimal values. I had to modify it to work for ints :P – TheTXI Apr 27 '09 at 13:27