JavaScript - Test for an integer

前端 未结 11 2030
醉梦人生
醉梦人生 2020-12-02 11:17

I have a text field that allows a user to enter their age. I am trying to do some client-side validation on this field with JavaScript. I have server-side validation already

11条回答
  •  离开以前
    2020-12-02 11:33

    If your number is in the 32bit integer range, you could go with something like:

    function isInt(x) { return ""+(x|0)==""+x; }
    

    The bitwise or operator forces conversion to signed 32bit int. The string conversion on both sides ensures that true/false want be matched.

提交回复
热议问题