27

I have this javascript function to validate if a number is greater than another number

function validateForm() {
    var x = document.forms["frmOrder"]["txtTotal"].value;
    var y = document.forms["frmOrder"]["totalpoints"].value;

    if (x > y) {
        alert("Sorry, you don't have enough points");
        return false;
    }
}

It's not working for some reason.

If I do alert(x) I get 1300, and alert(y) gives 999

This works....

function validateForm() {
    var x = 1300;
    var y = 999;

    if (x > y) {
        alert("Sorry, you don't have enough points");
        return false;
    }
}
7
  • 4
    You're comparing strings, not numbers. Commented Oct 26, 2012 at 1:31
  • 2
    @MattStone: "1300" > "999"; // false Commented Oct 26, 2012 at 1:32
  • 1
    @user1689607 "user1689607" > "Matt Stone" // true - I'll have to look up the exact rules for the string conversion. Strange that single digit number strings work correctly. Commented Oct 26, 2012 at 1:35
  • 3
    @MattStone, string comparison compares the strings letter by letter beginning with the first, 1 is less than 9, which is why '1300' < '999'. Commented Oct 26, 2012 at 1:36
  • 1
    Ah right, so it's still technically a string (ASCII order) comparison. Thanks guys. Commented Oct 26, 2012 at 1:38

4 Answers 4

72

You should convert them to number before compare.

Try:

if (+x > +y) {
  //...
}

or

if (Number(x) > Number(y)) {
  // ...
}

Note: parseFloat and pareseInt(for compare integer, and you need to specify the radix) will give you NaN for an empty string, compare with NaN will always be false, If you don't want to treat empty string be 0, then you could use them.

Sign up to request clarification or add additional context in comments.

Comments

3

You're comparing strings. JavaScript compares the ASCII code for each character of the string.

To see why you get false, look at the charCodes:

"1300".charCodeAt(0);
49
"999".charCodeAt(0);
57

The comparison is false because, when comparing the strings, the character codes for 1 is not greater than that of 9.

The fix is to treat the strings as numbers. You can use a number of methods:

parseInt(string, radix)
parseInt("1300", 10);
> 1300 - notice the lack of quotes


+"1300"
> 1300


Number("1300")
> 1300

Comments

3

You can "cast" to number using the Number constructor..

var number = new Number("8"); // 8 number

You can also call parseInt builtin function:

var number = parseInt("153"); // 153 number

2 Comments

Ooops, using Number as a constructor returns a Number object, and an object is only ever "equal" to itself, so new Number('7') == new Number('7') returns false. I think you meant to use it as a function, so Number('7') == Number('7'). And parseInt should always use a radix: parseInt('153',10).
Ohh!, you are right @RobG about the "=="!! .. I tested these cases and were fine, and I suposed that Number object was always being calling "toString()" implicitly (actually, I think internally its calling valueOf() method): new Number('725') < new Number('800'); //Outputs true new Number('91') > new Number('90'); //Outputs true new Number('7') < new Number('7'); //Outputs false new Number('7') > new Number('8'); //Outputs false new Number('8') < new Number('7'); //Outputs false But for equals, in this way, you need: new Number('7').valueOf() == new Number('7').valueOf(); //Outputs true
1

Do this.

var x=parseInt(document.forms["frmOrder"]["txtTotal"].value);
var y=parseInt(document.forms["frmOrder"]["totalpoints"].value);

1 Comment

Specify that you want base 10 with ...]["txtTotal"].value, 10);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.