我想只允许文本输入字段中的数字。所以这就是我所做的:
$(document).on("keypress", ".numeric", function (e) {
if(($(this).attr("class")).indexOf("price") >= 0) {
if (e.which != 8 && e.which != 0 && e.which != 190 && (e.which < 48 || e.which > 57)) {
//display error message
$(this).css("background-color", "rgb(245, 203, 203)");
$(this).css("border", "1px solid #FF0000");
$(this).attr("placeholder", "Enter only numbers");
return false;
}
}
}
我已添加e.which != 190
以允许十进制数字,但它不起作用。事件如果有效,它将允许用户输入12.9.32.
这样的错误。
你对此有任何想法吗?谢谢
答案 0 :(得分:0)
我正在使用以下代码来准确描述您所描述的内容。它只允许一个十进制符号(或。或)
$elm.on('keypress', function(e){
//Only allow 0-9, '.' and backspace (charCode 0 in Firefox)
if(e.charCode == 46 || e.charCode == 44){
var currentContents = $input.val();
return !(currentContents.indexOf(',') !== -1 || currentContents.indexOf('.') !== -1);
}
return (e.charCode >= 48 && e.charCode <= 57) || e.charCode === 0;
})
答案 1 :(得分:0)
根据评论的建议我解决了我的问题,使用Regex表达式使一个通用代码独立于键盘类型,我发布此解决方案,以防有兴趣或帮助某人。
$(document).on("keypress", ".numeric", function (e) {
if(($(this).attr("class")).indexOf("price") >= 0) {
var price = String.fromCharCode(e.which);
//if the letter is not digit then display error and don't type anything
if(/^[0-9]+\.?[0-9]*$/.test($(this).val() + price) == false) {
$(this).css("background-color", "rgb(245, 203, 203)");
$(this).css("border", "1px solid #FF0000");
$(this).attr("placeholder", "Enter only numbers");
return false;
}
else {
$(this).css("background-color", "#fff");
$(this).removeAttr( "placeholder");
}
}
});
我使用Keypress
不允许用户输入不符合我的正则表达式的内容。由于使用了按键事件,输入的字符尚未写入,我们无法使用$(this).val()
获取整个输入值,使用它我们只会获得旧输入值,这就是我们应该这样做的原因使用:String.fromCharCode(e.which)
捕获输入的char,然后将其与输入$(this).val() + price
谢谢。
答案 2 :(得分:0)
这是实现仅整数html文本字段的最强方法(将integerOnly类应用于input元素):
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Tabs - Default functionality</title>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
#tabs{
}
#vartabs .ui-widget-header {
background-color: white;
background-image: none;
border-top: none;
border-right: none;
border-left: none;
}
#vartabs .ui-widget-content {
background-color: white;
background-image: none;
}
#vartabs .ui-corner-top,#tabs .ui-corner-bottom,#tabs .ui-corner-all{
border-top-left-radius: 0px;
border-top-right-radius: 0px;
border-bottom-left-radius: 0px;
border-bottom-right-radius: 0px;
}
#vartabs .ui-state-default,
#vartabs .ui-state-default a {
background-color: lightgray;
text-align: center;
}
#vartabs .ui-state-default a {
width: 60px;
}
#vartabs .ui-tabs-active,
#vartabs .ui-tabs-active a {
background-color: darkgray;
text-align: center;
}
</style>
<script>
$(function() {
$( "#vartabs" ).tabs();
});
</script>
</head>
<body>
<div id="vartabs">
<ul>
<li><a href="#tabs-1">S</a></li>
<li><a href="#tabs-2">T</a></li>
<li><a href="#tabs-3">P</a></li>
</ul>
<div id="tabs-1">
<p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum. Nunc tristique tempus lectus.</p>
</div>
<div id="tabs-2">
<table id="summary-table" cellpadding="0" cellspacing="0" style="width:100%;">
<tr bgcolor="silver" valign="top">
<th width="60" height="40" ><font color="white">Col1</font></th>
<th width="50" height="40" ><font color="white">Count</font></th>
</tr>
<tr><td><a HREF="javascript::void()"><font color="black">Link_Text</font></a></td><td>3</td></tr>
</table>
</div>
<div id="tabs-3">
<p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p>
<p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum. Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>
</div>
</div>
</body>
</html>