只控制input框只能输入数字,并控制小数位数
2016-06-17 14:22
453 查看
<input type="text" id="phone" name="phone" class="text"
onkeyup="extractNumber(this, 0, false);"/>
extractNumber(this, 0, false),中间的参数就是这只小数点后的位数
function extractNumber(obj, decimalPlaces, allowNegative) {
var temp = obj.value;
// avoid changing things if already formatted correctly
var reg0Str = '[0-9]*';
if (decimalPlaces > 0) {
reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
} else if (decimalPlaces < 0) {
reg0Str += '\\.?[0-9]*';
}
reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
reg0Str = reg0Str + '$';
var reg0 = new RegExp(reg0Str);
if (reg0.test(temp)) return true;
// first replace all non numbers
var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
var reg1 = new RegExp(reg1Str, 'g');
temp = temp.replace(reg1, '');
if (allowNegative) {
// replace extra negative
var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
var reg2 = /-/g;
temp = temp.replace(reg2, '');
if (hasNegative) temp = '-' + temp;
}
if (decimalPlaces != 0) {
var reg3 = /\./g;
var reg3Array = reg3.exec(temp);
if (reg3Array != null) {
// keep only first occurrence of .
// and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
reg3Right = reg3Right.replace(reg3, '');
reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
temp = temp.substring(0, reg3Array.index) + '.' + reg3Right;
}
}
obj.value = temp;
}
onkeyup="extractNumber(this, 0, false);"/>
extractNumber(this, 0, false),中间的参数就是这只小数点后的位数
function extractNumber(obj, decimalPlaces, allowNegative) {
var temp = obj.value;
// avoid changing things if already formatted correctly
var reg0Str = '[0-9]*';
if (decimalPlaces > 0) {
reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
} else if (decimalPlaces < 0) {
reg0Str += '\\.?[0-9]*';
}
reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
reg0Str = reg0Str + '$';
var reg0 = new RegExp(reg0Str);
if (reg0.test(temp)) return true;
// first replace all non numbers
var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
var reg1 = new RegExp(reg1Str, 'g');
temp = temp.replace(reg1, '');
if (allowNegative) {
// replace extra negative
var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
var reg2 = /-/g;
temp = temp.replace(reg2, '');
if (hasNegative) temp = '-' + temp;
}
if (decimalPlaces != 0) {
var reg3 = /\./g;
var reg3Array = reg3.exec(temp);
if (reg3Array != null) {
// keep only first occurrence of .
// and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
reg3Right = reg3Right.replace(reg3, '');
reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
temp = temp.substring(0, reg3Array.index) + '.' + reg3Right;
}
}
obj.value = temp;
}
相关文章推荐
- Android 实现模糊半透明效果的简单实现
- 微信测试帐号如何设置URL和Token,以及相关验证的原理
- Matlab学习笔记之安装教程
- RedHat 7配置bonding双网卡绑定
- RedHat 7配置bonding双网卡绑定
- 互联网需要什么样的人-从需求看一个人
- Android 短信的备份
- EF6增改删等常用基类
- (转) 在Eclipse中进行C/C++开发的配置方法(20140721最新版)
- eclipse导入Maven父子项目出现的错误及解决方案
- diy数据库(八)--客户端和服务器之间的通信协议
- Jemter 远程连接多台pc测试
- mybatis sql in 查询 mapper与service层写法
- Android学习笔记六之Service二
- 第11章 继承和多态
- VS平台工具集版本
- Protobuf 之java使用篇
- 分布式大牛-likehua
- JavaScript 闭包??
- 闲话落地细无声