input框输入限制

xiaoxiao2021-02-28  48

js限制输入框只能输入数字

分享下js限制输入框中只能输入数字的方法,包括整数与小数,分享几个例子,有需要的朋友参考下。

1.使用正则表达式限制输入框只能输入数字:

1 <input type="text" onkeyup="this.value=this.value.replace(/[^\d]/g,'') " onafterpaste="this.value=this.value.replace(/[^\d]/g,'') " name="f_order" value="1"/>

其中,onafterpaste防止用户从其它地方copy内容粘贴到输入框。

 

2.输入框只能输入字母和下横线的正则表达式

1 <input onkeyup="this.value=this.value.replace(/[^_a-zA-Z]/g,'')" onpaste="this.value=this.value.replace(/[^_a-zA-Z]/g,'')">

 

3.输入框只能输入字母数字和下横线的正则表达式  

1 <input onkeyup="this.value=this.value.replace(/[^\w]/g,'')" onpaste="this.value=this.value.replace(/[^\w]/g,'')">

或者

1 <input onkeyup="this.value=this.value.replace(/[\W]/g,'')" onpaste="this.value=this.value.replace(/[\W]/g,'')">
转载请注明原文地址: https://www.6miu.com/read-2625533.html

最新回复(0)