javascript if number greater than number

后端 未结 4 1829
梦毁少年i
梦毁少年i 2020-11-29 04:38

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

function validateForm() {
    var x = document.forms[\"frmOrder\"][\"t         


        
4条回答
  •  野性不改
    2020-11-29 05:06

    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.

提交回复
热议问题