2012-12-18 39 views
0

的範圍內amount..I要防止用戶進入量超過一定amount..I更大用ajax ..但它不工作的方式試着我想..我想jquery wud做需要..但我不擅長它..如果任何人都可以幫忙? Ajax的功能我寫了:jQuery來限制我有一個文本框,輸入需要輸入

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

所以基本上,你試圖驗證訂單數量與實際庫存,對不對? –

+0

是絕對... – Sharvari

+0

然後,保健佳品我們什麼'limit_input.php'迴歸樣子?爲什麼你將'max'作爲輸入而不是輸出? –

回答

1
$('input').on('keyup', function(){ 
    if($(this).val() > someNumber){ 
     $(this).prop('disabled', true); 
     alert('You cannot enter that many characters.'); 
    } 
}); 
+0

OP正在限制值不是長度。刪除'.length'就可以了。 –

+0

這裏代碼shud被寫入..Sryry我知道jquery – Sharvari

+0

我wud是偉大的,如果你幫我出 – Sharvari

1

你試圖使用maxLength

<input maxLength="10"/> 
+0

我dnt想要長度..實際值shud不是exceec – Sharvari

0

它會爲你的任務很有幫助。

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

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

我想要限制長度...我想要的值不要超過一定的限制 – Sharvari

+0

@Sharvari - 你是什麼意思*不超過一定的限制*? –

+0

對不起!我真的不明白你真正想要什麼。 –

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> 
+0

這個問題是我有二硝基甲苯硬編碼最大值 – Sharvari

+0

您可以在對象傳遞對客戶端的規則。 –