Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Jquery来限制输入的范围_Php_Javascript_Jquery_Ajax - Fatal编程技术网

Php Jquery来限制输入的范围

Php Jquery来限制输入的范围,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,我有一个文本框,将输入作为金额。我想阻止用户输入大于某个金额的金额。我尝试使用ajax。但它没有按我想要的方式工作。我认为jquery可以做必要的工作。但我不太擅长。如果有人能帮上忙的话?? 我编写的Ajax函数: function maxIssue(max, input, iid) { var req = getXMLHTTP(); var strURL = "limit_input.php?max=" + max + "&iid=" + iid; if (in

我有一个文本框,将输入作为金额。我想阻止用户输入大于某个金额的金额。我尝试使用ajax。但它没有按我想要的方式工作。我认为jquery可以做必要的工作。但我不太擅长。如果有人能帮上忙的话?? 我编写的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);
}
函数maxIssue(最大,输入,iid){
var req=getXMLHTTP();
var strURL=“limit_input.php?max=“+max+”&iid=“+iid;
如果(输入>最大值){
警报(“出库失败,库存数量为”+最大值);
}
如果(输入<0){
警报(“发布失败。输入正值”);
}
如果(请求){
req.onreadystatechange=函数(){
如果(req.readyState==4){
//只有在“OK”的情况下
如果(请求状态==200){
document.getElementById('maxdiv')。innerHTML=req.responseText;
}否则{
警报(“使用XMLHTTP:\n“+req.statusText时出现问题”);
}
}
}
}
请求打开(“获取”,strURL,true);
请求发送(空);
}

您是否尝试过使用
maxLength

<input maxLength="10"/>

这将有助于您完成任务

<input type="text" name="usrname" maxlength="10" />

function limitText(field, maxChar){
    $(field).attr('maxlength',maxChar);
}

函数limitText(字段,maxChar){
$(字段).attr('maxlength',maxChar);
}

使用jquery验证非常简单。您必须定义要显示以进行验证的规则和消息,并将其附加到表单元素中

本教程非常有帮助

这里有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>

验证试验
$(文档).ready(函数(){
$(“#测试验证”).validate({
规则:{
标题:“必选”,
说明:{
要求:正确,
最大长度:4000
}
},
信息:{
标题:“需要标题”,
说明:{
必填:“需要说明”,
maxlength:“应少于4000个字符”
}
}
});
});
#testvalidation.error{
颜色:#FB3A3A;
字体大小:300;
} 


OP希望限制值而不是长度。删除
.length
就可以了。如果你能帮助我,我将不胜感激out@Sharvari此代码位于
标记之间,或在javascript文档中。它还需要用
$(function(){//mycode})封装以便在加载DOM后委派事件。@Alex R.我进行了调整。我仍然不知道OP是否希望这样,但不管怎样,都应该没有问题。@ohgod为什么要检查我在问题中的评论。我想他是想在服务器端验证一些东西,而不仅仅是输入长度。我不想要长度。。实际值不应超出限制长度的范围。。。我希望该值不超过某个特定值limit@Sharvari-你说不超过某个限度是什么意思?对不起!我真的不明白你到底想要什么。所以,基本上,你是在验证订单数量与实际库存,对吗?那么,要不要跟我们分享一下
limit\u input.php
的退货情况?为什么要将
max
作为输入而不是输出传递?limit\u input.php将文本框的值设置为最大值,您可以在客户端的规则对象中传递该值。
<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>