HTML text input allow only numeric input

前端 未结 30 3527
孤街浪徒
孤街浪徒 2020-11-21 04:58

Is there a quick way to set an HTML text input () to only allow numeric keystrokes (plus \'.\')?

30条回答
  •  醉梦人生
    2020-11-21 05:18

    I saw some great answers however I like them as small and as simple as possible, so maybe someone will benefit from it. I would use javascript Number() and isNaN functionality like this:

    if(isNaN(Number(str))) {
       // ... Exception it is NOT a number
    } else {
       // ... Do something you have a number
    }
    

    Hope this helps.

提交回复
热议问题