0
A
回答
3
<input type="text" name="fieldname" maxlength="10">
您可以使用maxlenght
屬性來禁止輸入比預期更多的字符。你不需要JS。
如果你還是想用JS的話:
$("#myformid").keypress(function() {
if($(this).val().length > 10) {
//display your warinig the way you chose
}
]
});
1
相信這是可能的! 首先,計算輸入的字符,然後打印出你想要的信息(這裏是:「剩下的字符:X」,在我的例子中最大長度爲100)。
這是JSFiddle!
HTML:
<textarea rows="4" cols="50" id ="yourtextarea">
</textarea>
<div id="info"></div>
JS:
$("#yourtextarea").keyup(function(){
$("#info").text("Characters left: " + (100 - $(this).val().length));
});
0
function check_length(my_form)
{
maxLen = 50; // max number of characters allowed
if (my_form.my_text.value.length >= maxLen) {
var msg = "You have reached your maximum limit of characters allowed";
alert(msg);
my_form.my_text.value = my_form.my_text.value.substring(0, maxLen);
}
}
<textarea onKeyPress=check_length(this.form); onKeyDown=check_length(this.form); name=my_text rows=4 cols=30></textarea>
0
下面就來驗證用戶輸入,而無需使用Jquery的代碼。
<textarea id=txt onKeyUp=check() maxlength=10>
Abc
</textarea>
<div id=warning></div>
<script>
function check() {
stringLength = document.getElementById('txt').value.length;
if (stringLength >= 10) {
document.getElementById('warning').innerText = "Maximum characters are 10"
} else {
document.getElementById('warning').innerText = ""
}
}
</script>
相關問題
- 1. 用戶輸入最大長度處理
- 2. 長文本字段的最大長度
- 3. html輸入文本字段的「value」屬性的最大長度
- 4. 輸入到gmp整數的字符串的最大長度?
- 5. HTTP消息的最大長度?
- 6. 防止用戶在達到最大長度時輸入其他字符
- 7. 覆蓋MVC中的最大長度錯誤消息
- 8. 如何增加MySQL錯誤消息的最大長度?
- 9. 定義HTML文本區域的最大長度
- 10. Kentico CMS:文本區域輸入長度計算並確定最大長度 - 特別是當有文本換行
- 11. 文本區最大長度不工作
- 12. 域名的最大長度
- 13. 字符數組的最大長度
- 14. 是否有用戶可以輸入的最大整數長度?
- 15. 最大進入表單輸入文本區域的URL驗證
- 16. 設置文本區域中字符的最大數量
- 17. TinyGP的最大輸入長度/可變輸入長度
- 18. UUID最大字符長度
- 19. 數據輸入最大字符串長度指南
- 20. 文本區域助手,如何設置最大長度
- 21. 什麼是最大SMS消息長度?
- 22. adb logcat:增加最大消息長度
- 23. 「tzname」字段/時區標識符名稱的最大長度
- 24. 單獨的錯誤消息的最小和最大字符串長度 - MVC4數據註釋
- 25. 在文本字段的最大長度上調用javascript函數
- 26. 輸入字段,只接受最大長度的數字
- 27. 用於cakephp散列函數的最大字符串長度
- 28. UIAlertView最大文本長度
- 29. 錯誤的輸入文本值長度
- 30. 最大長度輸入文本的Javascript驗證
我已經這樣做了,但是如果用戶試圖輸入更多的信息,我們想要彈出一個錯誤消息 – discodowney
然後你需要javascript。那裏有無數的驗證庫。 – dmoo
@discodowney添加了js代碼來檢測輸入了多少個字符。我不知道,你想怎麼顯示你警告你,所以我把這部分留空 –