How do I check if a JavaScript parameter is a number?

后端 未结 6 1524
一整个雨季
一整个雨季 2021-02-20 13:47

I\'m doing some trouble-shooting and want to add a check that a parameter to a function is a number. How do I do this?

Something like this...

function fn         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-20 14:22

    function fn(id) {
        var x = /^(\+|-)?\d+$/;
        if (x.test(id)) {
            //integer
            return true;
        }
        else {
            //not an integer
            return false;
        }
    }
    

    Test fiddle: http://jsfiddle.net/xLYW7/

提交回复
热议问题