input框输入文本限制
生活随笔
收集整理的這篇文章主要介紹了
input框输入文本限制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、文本框只能輸入數字(小數點也不能輸入)
<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')" />2、只能輸入數字,能輸小數點
①方式一:
② 方式二
<input type="text" t_value="" o_value="" onkeypress="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onkeyup="if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/))this.value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=this.value" onblur="if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))this.value=this.o_value;else{if(this.value.match(/^\.\d+$/))this.value=0+this.value;if(this.value.match(/^\.$/))this.value=0;this.o_value=this.value}"/>3、只能輸入字母和漢字
<input onkeyup="value=value.replace(/[\d]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\d]/g,''))" maxlength="10" name="Numbers"/>4、只能輸入英文字母和數字,不能輸入中文
<input onkeyup="value=value.replace(/[^\w\.\/]/ig,'')"/>5、小數點后只能有最多兩位(數字,中文都可輸入),不能輸入字母和運算符號
<input onkeypress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false"/>6、小數點后只能有最多兩位(數字,字母,中文都可輸入),可以輸入運算符號
<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')"/>7、輸入中文
<input type="text" onkeyup="this.value=this.value.replace(/[^\u4e00-\u9fa5]/g,'')"/>8、輸入英文
<input type="text" onkeyup="this.value=this.value.replace(/[^a-zA-Z]/g,'')"/>9、輸入數字、英文、中文
<input onkeyup="value=value.replace(/[^\w\u4E00-\u9FA5]/g, '')"/>在此,小編特別奉送一個不常用的小知識。當我們設置input框的placeholder屬性時,該屬性的樣式已經被默認設置,若是想改變改屬性默認的樣式咋辦呢,ok,下邊的方式就可以幫到你
input::-webkit-input-placeholder{// 樣式 } input::-moz-placeholder{// 樣式 } input:-moz-placeholder{// 樣式 } input::-ms-input-placeholder{// 樣式 }注:input只是小編在此統一設置,這一偽類也是可以支持各類選擇器的
總結
以上是生活随笔為你收集整理的input框输入文本限制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么通常在发送数据埋点请求的时候使用的
- 下一篇: NB物联网之天翼物联(2)——profi