Home > Article > Web Front-end > js method for limiting the input value of the input box to floating point type
In some projects, for example, the amount uses floating point type. For input restrictions, please refer to the following
<input type="text" value="" onkeyup="only_num(this)" onblur="only_num(this)"> <script> function only_num(obj) { //得到第一个字符是否为负号 var num = obj.value.charAt(0); //先把非数字的都替换掉,除了数字和. obj.value = obj.value.replace(/[^\d\.]/g,''); //必须保证第一个为数字而不是. obj.value = obj.value.replace(/^\./g,''); //保证只有出现一个.而没有多个. obj.value = obj.value.replace(/\. { 2, } /g,'.'); //保证.只出现一次,而不能出现两次以上 obj.value = obj.value.replace('.','$#$').replace(/\./g,'').replace('$#$','.'); //如果第一位是负号,则允许添加 if(num == '-') { obj.value = '-'+obj.value; } }</script>
The above is the detailed content of js method for limiting the input value of the input box to floating point type. For more information, please follow other related articles on the PHP Chinese website!