Html 如何在textarea上应用最小值和最大值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18184791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to apply min and max on textarea?
提问by LeBlaireau
Is there a way in HTML5 forms to add a min and max character to a textarea? It seems I can apply it to a regular input using pattern.
有没有办法在 HTML5 表单中向 textarea 添加最小和最大字符?看来我可以使用模式将它应用于常规输入。
<input pattern=".{3,}" title="3 characters minimum">
Am I looking at a jquery solution?
我在看 jquery 解决方案吗?
回答by ladriangb
HTML5 solution, min 5, max 20 characters
HTML5 解决方案,最少 5 个,最多 20 个字符
just set the attribute maxlength="20" and minlength="5" to the textarea tag
只需将属性 maxlength="20" 和 minlength="5" 设置为 textarea 标记
http://jsfiddle.net/xhqsB/603/
http://jsfiddle.net/xhqsB/603/
<form>
<textarea maxlength="20" minlength="5"></textarea>
<input type="submit" value="Check"></input>
</form>
回答by SohelAhmedM
For max:see below code
Use maxlength
for maximum character.
马克斯:请参见下面的代码
使用maxlength
的最大字符。
maxlength="nuber_of_characters"
Source: http://www.w3.org/TR/html5/forms.html#the-textarea-element
来源:http: //www.w3.org/TR/html5/forms.html#the-textarea-element
For min:source
Use Javascript as below:
use id attribute in <textarea>
. Set id attribute to minle(for eg) ie id="minle"
对于 min:source
使用 Javascript 如下:
在<textarea>
. 将 id 属性设置为 minle(例如)即id="minle"
<textarea rows="10" cols="80" maxlength="200" required id="minle" >
</textarea>
and put following js code segment in your form
tag
并将以下 js 代码段放入您的form
标签中
<form action="mango.php" method="post" id="form12" onsubmit="var text = document.getElementById('minle').value; if(text.length < 80) { alert('put more info!'); return false; } return true;">
This allow user to input no. of characters from 80 to 200.
这允许用户输入编号。80 到 200 个字符。
回答by Ali ?ar?k??o?lu
HTML5 solution, min 5, max 10 characters
HTML5 解决方案,最少 5 个,最多 10 个字符
http://jsfiddle.net/xhqsB/102/
http://jsfiddle.net/xhqsB/102/
<form>
<input pattern=".{5,10}">
<input type="submit" value="Check"></input>
</form>