我有一个文本框,输入为金额..我想阻止用户输入大于一定数量的金额..我尝试使用ajax ..但它不按我想要的方式工作..我认为jquery wud做必要的..但我不是很擅长..如果有人可以帮助?? 我写过的Ajax函数:
function maxIssue(max, input, iid) {
var req = getXMLHTTP();
var strURL = "limit_input.php?max=" + max + "&iid=" + iid;
if (input > max) {
alert("Issue Failed.Quantity Present in Stock is " + max);
}
if (input < 0) {
alert("Issue Failed.Enter positive Value");
}
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('maxdiv').innerHTML = req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
答案 0 :(得分:1)
$('input').on('keyup', function(){
if($(this).val() > someNumber){
$(this).prop('disabled', true);
alert('You cannot enter that many characters.');
}
});
答案 1 :(得分:1)
您是否尝试过使用maxLength
?
<input maxLength="10"/>
答案 2 :(得分:0)
这对你的任务很有帮助。
<input type="text" name="usrname" maxlength="10" />
function limitText(field, maxChar){
$(field).attr('maxlength',maxChar);
}
答案 3 :(得分:0)
使用jquery验证非常简单。您必须定义要显示以进行验证的规则和消息,并将其与表单元素一起附加。
本教程非常有帮助。
http://www.codeproject.com/Articles/213138/An-Example-to-Use-jQuery-Validation-Plugin
这是验证的101,如果你想尝试一下。我正在使用cdn,但您可以在路径中添加对库的引用。
<html>
<head>
<title>Validation Test</title>
<!-- Importing jquery and jquery ui library from cdn -->
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.js"></script>
<!-- End of Import -->
</head>
<body>
<script>
$(document).ready(function(){
$("#testvalidation").validate({
rules: {
title: "required",
description: {
required:true,
maxlength:4000
}
},
messages: {
title: "Title is Required",
description: {
required : "Description is Required",
maxlength: "Should be less than 4000 Characters"
}
}
});
});
</script>
<style>
#testvalidation .error{
color: #FB3A3A;
font-weight:300;
}
</style>
<form action=update.jsp class="form-horizontal" id="testvalidation">
<input name="title" type="text"></input><br>
<textarea name="description"></textarea>
<input type="submit" value="Submit">
</form>
</body>
</html>