问题描述
我有一个人可以输入价格到(XX.xx)的文本框。有没有办法确保他们只输入小数点后两位数? 解决方案
我有一个人可以输入价格到(XX.xx)的文本框。有没有办法确保他们只输入小数点后两位数? 解决方案
以下是主要类型
$ p $ $(document).ready(function(){
//仅用于数字整数
$($ body)on(keypress,.custom_numeric_int,function(e){
var keynum; $ b $ num_int_Exp = / ^ [0-9] + $ /;
$
if(window.event){// IE
keynum = e.keyCode;
} else
if(e.which){// Netscape / Firefox / Opera
keynum = e.which;
}
if(!String.fromCharCode(keynum).match(num_int_Exp))
{
return false;
}
返回true;
});
//仅用于数字浮动
$(body)。on(keypress,.custom_numeric_float,function (e){
//$('.custom_numeric_float').keypress(function(event)
/ /警报($(本).VAL()的indexOf() ); ($(this).val()。indexOf(。)> -1&& event.which == 46){
return false;
if ((event.which!= 46)&&(event.which< 48 || event.which> 57)){
event(
)
.preventDefault();
}
});
//用于字符输入
var charExp = / ^ [a-zA-Z] + $ /; (keypress,.custom_char,function(e){
var keynum;
if(window.event){// IE
keynum = e.keyCode;
} else
if(e.which){// Netscape / Firefox / Opera
keynum = e.which;
} $ b $如果(!String.fromCharCode(keynum).match(charExp))
{
返回false;
}
返回true;
});
//用于字母数字输入
var alphaExp = / ^ [a-zA-Z0-9] + $ /; (keypress,.custom_alphanumeric,函数(e){
var keynum;
if(window.event){// IE
keynum = e.keyCode;
} else
if(e.which){// Netscape / Firefox / Opera
keynum = e.which;
} $ b $如果(!String.fromCharCode(keynum).match(alphaExp))
{
return false;
}
return true;
});});
现在给您的文本框添加相应的类,然后完成验证。
希望这会有帮助。
I have a textfield that a person can input a price into (XX.xx). Is there a way to make sure they only put in no more than 2 digits after the decimal?
Here are the major types of validation.
$(document).ready(function(){
//for numeric integer only
var num_int_Exp = /^[0-9]+$/;
$("body").on("keypress", ".custom_numeric_int", function(e){
var keynum;
if(window.event){ // IE
keynum = e.keyCode;
}else
if(e.which){ // Netscape/Firefox/Opera
keynum = e.which;
}
if(!String.fromCharCode(keynum).match(num_int_Exp))
{
return false;
}
return true;
});
//for numeric float only
$("body").on("keypress", ".custom_numeric_float", function(e){
//$('.custom_numeric_float').keypress(function(event)
//alert($(this).val().indexOf("."));
if ($(this).val().indexOf(".") > -1 && event.which == 46) {
return false;
}
if ((event.which != 46) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});
//for character input
var charExp = /^[a-zA-Z]+$/;
$("body").on("keypress", ".custom_char", function(e){
var keynum;
if(window.event){ // IE
keynum = e.keyCode;
}else
if(e.which){ // Netscape/Firefox/Opera
keynum = e.which;
}
if(!String.fromCharCode(keynum).match(charExp))
{
return false;
}
return true;
});
//for alpha-numeric input
var alphaExp = /^[a-zA-Z0-9]+$/;
$("body").on("keypress", ".custom_alphanumeric", function(e){
var keynum;
if(window.event){ // IE
keynum = e.keyCode;
}else
if(e.which){ // Netscape/Firefox/Opera
keynum = e.which;
}
if(!String.fromCharCode(keynum).match(alphaExp))
{
return false;
}
return true;
});});
Now give the appropriate class to your textbox and you are done with validation.Hope this will help.
这篇关于Javascript / jQuery - 浮动验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!