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(關於正則的一些表達式)