JavaScript - Test for an integer

前端 未结 11 2064
醉梦人生
醉梦人生 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:42

    var intRegex = /^\d+$/;
    if(intRegex.test(someNumber)) {
       alert('I am an int');
       ...
    }
    

    That will absolutely, positively fail if the user enters anything other than an nonnegative integer.

提交回复
热议问题