input只能輸入數字,負數,小數(保留兩位)


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM