0

From user input, I receive a string which must contain numbers. 0 and empty string must be treated differently.

I've made this brief case study

0 == "" // true
parseInt(0) == "" // true
parseInt("") // NaN
parseInt("") == "NaN" // false
parseInt("") == NaN // false
typeof parseInt("") // "number"

With a bit of extra research I've made up this solution (considering uInput the user input)

function userInput(uInput){
var n = parseInt(uInput);
if (isNan(n))
// empty string
else
// 0 or number
}

It looks like it is able to distinguish 0 from empty string correctly in Google Chrome.

What I'd like to know is:

  • Is this the best/most efficient solution?
  • Is this solution compatible in any browser?
  • Is this even a solution at all? Is there any way n can become something other than a number (considering uInput could be any kind of string)?

I wouldn't be asking this, but since empty string and 0 are treated the same (?!) I don't know to which other holes is my code potentialy exposed.

Saturnix
  • 10,130
  • 17
  • 64
  • 120

6 Answers6

1

I would go with the ===, try it like: 0 === "" // false

The difference about them and about type conversions read:
Q: Does it matter which equals operator (== vs ===) I use in JavaScript comparisons?

Community
  • 1
  • 1
p1100i
  • 3,710
  • 2
  • 29
  • 45
1

I'd do something like this:

var nmbr = parseInt(uInput, 10);
if (!isNaN(nmbr)) {
    // Yay, valid input!
} else {
    // Ups, weird input...
}

This seems the simplest and most correct answer, because parseInt only returns an integer or NaN.

gnclmorais
  • 4,897
  • 5
  • 30
  • 41
  • This will say "3434 String" is valid input which is not :( – Adarsh Kumar Sep 27 '13 at 10:51
  • I was assuming @Saturnix wanted to just drop the string after the first numbers... `parseInt('3434 String'` will return **3434**, which seems fine to me. But, if you want to be more rigorous, you could use `Number(uInput)` to match with `parseInt(uInput, 10)` result, and check their differences. – gnclmorais Sep 27 '13 at 10:56
  • 1
    "3434 String" to become 3434 is ok to me - I just wanted to make sure that the final value I get is a number and not a string. I didn't tought about checking the possible return values of parseInt(). Since you proven me there can only be 2 (number or NaN) I'm ok with that. – Saturnix Sep 27 '13 at 12:14
0

Is this even a solution at all? Is there any way n can become something other than a number?

Yes. parseInt only does consider the first, integer part of the string when parsing it. It ignores the rest, so "12.34 oops" will still validate as 12.

I receive a string which must contain numbers

You can use a simple regex to test that:

/^\d+$/.test(uInput)

It will yield true for strings that consists of only (and at least one) digits, and false for anything else (non-numeric and empty strings).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Create your own test, you do like a million try for each and retrieve the time it takes.

For browser compatibility, I guess simple operation should be alike.

Michael Laffargue
  • 10,116
  • 6
  • 42
  • 76
0

Try this:

function validate(uInput){
    var regEx = /^[1-9]{1}[\d]*$/;
    if (regEx.test(uInput))
        alert(uInput + " is a valid number.");
    else
        alert(uInput + " is not valid number.");

    var validNumber = parseInt(uInput);
}
Adarsh Kumar
  • 1,134
  • 7
  • 21
0
0 == "" // true

because of == which does a type coercion first:

0 == false, "" == false so false == false.

you should be using ===, where

0 === "" // false

without type coercion.

itmitica
  • 501
  • 3
  • 10