0

I need to make sure that my string is a number and for doing that, I was using isNaN function and everything was working but I got a problem when I typed in my input field '0e1'.

I was wondering what's the best way to check if my string is a number and without scientific notation

alexiscrack3
  • 148
  • 1
  • 7

4 Answers4

2

Try using regex. Here are some examples. The result is null if no match was found.

alert("0e1".match(/^[-+]?[0-9]*\.?[0-9]+$/))    // null
alert("1".match(/^[-+]?[0-9]*\.?[0-9]+$/))      // 1
alert("-1".match(/^[-+]?[0-9]*\.?[0-9]+$/))     // -1
alert("1.0".match(/^[-+]?[0-9]*\.?[0-9]+$/))    // 1.0
alert("-1.5".match(/^[-+]?[0-9]*\.?[0-9]+$/))   // -1.5
alert("-1.5 4".match(/^[-+]?[0-9]*\.?[0-9]+$/)) // null
Chris
  • 2,766
  • 1
  • 29
  • 34
0

If you want your input to only be an integer or float do this instead;

var input = '0e1';

if(!isNaN(input) && (parseInt(input, 10).toString() == input || parseFloat(input).toString() == input)){
    //This is a valid number
}
Preston S
  • 2,751
  • 24
  • 37
0

Actually, the method you are using is fine. You have one problem with your code:

isNaN() should be !isNaN(). Remember, isNaN() checks if the number is NotaNumber. `!isNaN() checks if it IS a number.

isNaN(0e1) should return false because 0e1 is a number! I know, it's kind of confusing.

!isNaN(0e1) will return TRUE because !isNan() checks if a value IS a number.

I hope this helps.

Ian Hazzard
  • 7,661
  • 7
  • 34
  • 60
  • The OP is trying to check if my string is a number **without scientific notation**. In addition `isNaN` will fail in some edge cases like `!isNaN('') === true`, this should be the correct way to implement an `IsNumeric` function: http://stackoverflow.com/a/1830844/63011 (but it's not what the OP is asking) – Paolo Moretti Nov 19 '14 at 22:56
  • I would disagree with you. It's hard to tell from the way the OP asked the question, but I think he DOES want to use scientific notation. Otherwise he wouldn't of mentioned it. – Ian Hazzard Nov 19 '14 at 23:02
  • quoting his words: *check if my string is a number and without scientific notation* – Paolo Moretti Nov 19 '14 at 23:03
  • His question has been edited since I posted my answer. – Ian Hazzard Nov 19 '14 at 23:09
  • @PaoloMoretti, quote from original question: *I was using isNaN function but it's not working maybe because it looks like scientific notation.* – Ian Hazzard Nov 19 '14 at 23:10
  • His question was edited before you posted your answer, but I agree that it's not clear :) – Paolo Moretti Nov 19 '14 at 23:17
  • @PaoloMoretti, yes I see that, but I wrote it before and forgot to hit submit. – Ian Hazzard Nov 19 '14 at 23:18
0

isNaN() function determines whether a value is an illegal number, so if it is not a number. The good thing about isNaN function is, that is suportet by all browsers (ch, ie, ff, op, sf).

function myFunction() {
    var a = isNaN(123) + "<br>";
    var b = isNaN(-1.23) + "<br>";
    var c = isNaN(123-456) + "<br>";
    var d = isNaN("Hello") + "<br>";
    var e = isNaN("2014/11/19") + "<br>";

    var result = a + b + c + d + e;
    document.getElementById("test").innerHTML = result;
}

So result would be: If value is a number return "False" if is not a number return "True".

You can test it on JsFiddle: isNaN()

DaniKR
  • 2,418
  • 10
  • 39
  • 48