[英]jquery regular expression (allow minus, digits, dot)
需要有关 jquery 中正则表达式的帮助。 这段代码只允许输入一个点和数字,但要让减号在开头(对于负值)
$(function(){
$('.number_dots').on('input', function(){
this.value = this.value.replace(/^\.|[^\d\.]|\.(?=.*\.)|^0+(?=\d)/g, '');
});
});
已解决 解决办法:
$(function(){
$('.number_dots').on('input', function(){
//this.value = this.value.replace(/^\.|[^\d\.]|\.(?=.*\.)|^0+(?=\d)/g, '');
var value = this.value;
value = value.trim();
//If minus symbol occur at the beginning
if(value.charAt(0) === '-'){
value = value.substring(1, value.length);
value = "-"+value.replace(/^\.|[^\d\.]|\.(?=.*\.)|^0+(?=\d)/g, '');
}else{
value = value.replace(/^\.|[^\d\.]|\.(?=.*\.)|^0+(?=\d)/g, '');
}
console.log(value);
this.value = value;
});
});
此正则表达式 ( ^-?[0-9]\\d*(\\.\\d+)?$
) 匹配正确的 +/- 十进制,并删除不正确的额外数字,请使用以下代码。
var val="-12XXX.0abc23"; val = val.replace(/^\\.|[^-?\\d\\.]|\\.(?=.*\\.)|^0+(?=\\d)/g, ''); console.log(val);
你能试试下面的代码吗
var value = '-423423.44';
value = value.trim();
//If minus symbol occur at the beginning
if(value.charAt(0) === '-'){
value = value.substring(1, value.length);
value = "-"+value.replace(/^\.|[^\d\.]|\.(?=.*\.)|^0+(?=\d)/g, '');
}else{
value = value.replace(/^\.|[^\d\.]|\.(?=.*\.)|^0+(?=\d)/g, '');
}
console.log(value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.