A text box to enter numbers, letters, text box shielding paste only numbers, letters, shielding paste

A text box to enter numbers, letters, shielding paste

 

Text box input limit, so that the text input box can (or can not input) numbers / letters / characters and the like.
Many methods of operation, the main use of regular expressions, onkeyup, onafterpaste.
In the case where more than these circumstances may demand, or cross needs to be modified according to different conditions to achieve the corresponding effect on their own statements, it remains the same.


 

只能输入数字(整数,屏蔽非法粘贴)
<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">

 

只能输入数字(可以有小数点)
<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')" />

 

只能输入数字(整数)
<input onkeyup="JavaScript:this.value=this.value.replace(/\D/gi,'')" />

 

只能输入字母和汉字(屏蔽非法粘贴)
<input onkeyup="value=value.replace(/[\d\.]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\d\.]/g,''))"/>

 

只能输入字母和数字(屏蔽非法粘贴)
<input onkeyup="value=value.replace(/[\W]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\W]/g,''))" />

 

小数点后只能有最多两位(数字,可以有正负号)
<input onkeyup="if(!/^[+-]*(\d)*(\.\d{0,2})*$/.test(value)) this.value='';" onafterpaste="if(!/^[+-]*(\d)*(\.\d{0,2})*$/.test(value)) this.value='';">
 
不能输入中文
<input onkeyup="value=value.replace(/[\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\u4E00-\u9FA5]/g,''))" />

 

不能输入全角标点符(可以有汉字)
<input onkeyup="value=value.replace(/[\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\uFF00-\uFFFF]/g,''))" />

Transfer: https://www.cnblogs.com/zhq195/p/4974628.html

Text box input limit, so that the text input box can (or can not input) numbers / letters / characters and the like.
Many methods of operation, the main use of regular expressions, onkeyup, onafterpaste.
In the case where more than these circumstances may demand, or cross needs to be modified according to different conditions to achieve the corresponding effect on their own statements, it remains the same.


 

只能输入数字(整数,屏蔽非法粘贴)
<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">

 

只能输入数字(可以有小数点)
<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')" />

 

只能输入数字(整数)
<input onkeyup="JavaScript:this.value=this.value.replace(/\D/gi,'')" />

 

只能输入字母和汉字(屏蔽非法粘贴)
<input onkeyup="value=value.replace(/[\d\.]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\d\.]/g,''))"/>

 

只能输入字母和数字(屏蔽非法粘贴)
<input onkeyup="value=value.replace(/[\W]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\W]/g,''))" />

 

小数点后只能有最多两位(数字,可以有正负号)
<input onkeyup="if(!/^[+-]*(\d)*(\.\d{0,2})*$/.test(value)) this.value='';" onafterpaste="if(!/^[+-]*(\d)*(\.\d{0,2})*$/.test(value)) this.value='';">
 
不能输入中文
<input onkeyup="value=value.replace(/[\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\u4E00-\u9FA5]/g,''))" />

 

不能输入全角标点符(可以有汉字)
<input onkeyup="value=value.replace(/[\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\uFF00-\uFFFF]/g,''))" />

Guess you like

Origin www.cnblogs.com/shiyi2014/p/12461351.html