HTML text input allow only numeric input

前端 未结 30 3422
孤街浪徒
孤街浪徒 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:40

    You can also compare input value (which is treated as string by default) to itself forced as numeric, like:

    if(event.target.value == event.target.value * 1) {
        // returns true if input value is numeric string
    }
    

    However, you need to bind that to event like keyup etc.

提交回复
热议问题