当前位置: 首页 > 工具软件 > InputText > 使用案例 >

input text 限制文本内容

郜彦
2023-12-01

1、只允许输入1个字符

<input type="text" maxlength="1" > 

2、只允许输入数字

方法1:<input type="text"  oninput="this.value=this.value.replace(/[^0-9]+/,'')"/>
方法2:<input type="text"  oninput="this.value=this.value.replace(/[^\d]/g,'')"/>

3、只允许输入数字和字母

方法1:<input type="text" oninput="this.value=this.value.replace(/[^A-Za-z0-9]+/,'')" />
方法2:<input oninput="value=value.replace(/[^\d|chun]/g,'')">  
方法3:<input oninput="value=value.replace(/[^\w\.\/]/ig,'')">  
方法4:<input oninput="value=value.replace(/[\W]/g,'')">

4、只允许输入数字和“-”号

方法1:<input type="text" oninput="this.value=this.value.replace(/[^0-9-]+/,'')" />
方法2:<input type="text" oninput="this.value=this.value.replace(/[^\d-]/g,'')" />
方法3:<input type="text" oninput="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)" onblur="this.v();" />

5、只允许输入字母

<input type="text" oninput="this.value=this.value.replace(/[^a-zA-Z]/g,'')">  

6、只允许输入大小写字母、数字、下划线

<input type="text" oninput="this.value=this.value.replace(/[^\w_]/g,'');">

7、只允许输入中文

<input type="text" oninput="this.value=this.value.replace(/[^\u4e00-\u9fa5]/g,'')">  

8、允许输入中文、数字、英文

<input type="text"  oninput="value=value.replace(/[^\w\u4E00-\u9FA5]/g,'')" />

9、只允许输入数字,并只能保留两位小数

<input type="text" oninput="value=value.match(/\d+\.?\d{0,2}/,'')" />
 类似资料: