0

In the following program when only the White Spaces are entered, it shows ex2 exception value less than 5, instead of showing ex4 exception This is not a valid number, I couldn't understand the logic behind it.

<html>
<head>
    <title></title>
    <script type="text/javascript">
        function promptCheck() {

            var val=prompt("Enter a Number between 5 and 10","");

            try {
             if(val=="") {
                 throw "ex1";
             }

             else if(val<5) {
                 throw "ex2";
             }

             else if(val>10) {
                 throw "ex3";
             }

             else if(isNaN(val)) {
                 throw "ex4";
             }
            }

            catch(err) {
                if(err=="ex1") {
                    alert("You have not entered any value");
                }
                 if(err=="ex2") {
                    alert("Value less than 5");
                }
                 if(err=="ex3") {
                    alert("Value greater than 10");
                }
                 if(err=="ex4") {
                    alert("This is not a valid number");
                }
            }

        }
    </script>
</head>

<body>
    <input type="button" value="Bring Mouse on Me!" onmouseover="promptCheck()" />
</body>
</html>
sandbox
  • 2,631
  • 9
  • 33
  • 39

2 Answers2

2

In a numeric context, white-space convert to a zero. And zero is clearly lower than 5.

alert('  ' * 1);    // Shows 0

To solve the problem, you can use parseFloat, which will print NaN for spaces.
Another option is to use RegExps, to ensure that the input consists of integers:

var val = prompt("Enter a Number between 5 and 10", "");
val = /\d+/.exec(val); // Numbers if valid, null otherwise

// OR, instead of the previous line:
if (/\D/.test(val) { // If the input contains a non-digit character, error.
    throw "ex4";
} else ...

For string-to-number conversion, see this comparison of number-conversion methods. You can see what happens for a given input.

Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • It should be added that reordering the `else`s would solve the problem. – moteutsch Feb 11 '12 at 14:39
  • Ok... fair enough. Now what logic is applied to catch White Spaces? – sandbox Feb 11 '12 at 14:41
  • @moteutsch That will not solve the problem, because `isNaN` will check whether white-space, that is, zero, is `NaN`. That's false. – Rob W Feb 11 '12 at 14:41
  • Yes, but that's not exactly what I meant. I meant that you didn't actually give a solution, you only mentioned part of the problem. – moteutsch Feb 11 '12 at 14:47
  • @moteutsch Not a solution? Have you missed that block of code at the middle of my answer? Note: I have shown two possible solutions, on a separate line (up and down from `// OR, instead ... `. If you want to reject any non-number characters, use method two. – Rob W Feb 11 '12 at 14:49
  • I posted my original comment before you posted the second code sample. – moteutsch Feb 11 '12 at 14:54
2

That happens because string with only whitespaces is treated as empty string and it it is converted to 0.

So

"    "*1 // => 0

What you need to do is to parse value beforehand:

var value = parseInt(val, 10); // would be NaN in case of empty string
Ilya Tsuryev
  • 2,766
  • 6
  • 25
  • 29