js:
1 function upperCase(obj){//用户只能输入正负数与小数 2 if(isNaN(obj.value) && !/^-$/.test(obj.value)){ 3 obj.value=""; 4 } 5 if(!/^[+-]?\d*\.{0,1}\d{0,1}$/.test(obj.value)){ 6 obj.value=obj.value.replace(/\.\d{2,}$/,obj.value.substr(obj.value.indexOf('.'),3)); 7 } 8 }
html:
1 <input type='text' onkeyup='upperCase(this)' name='imin1' id='imin1'/>
<!--限制文本框只能输入正数、小数、负数-->
1 <input type="text" onkeyup="this.value=this.value.replace(/[^\-?\d.]/g,'')"/>
附:https://funteas.com/topic/592f828e572bfb9c4d965ab0(关于正则的一些表达式)