php 如何限制处理php的textarea字段中的字符数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10468680/
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 limit number of characters in a textarea field processing php?
提问by user1373168
I am using a a textarea field in a form that is made on html and the processing is done by php. I want to limit the number of characters the user can enter in the textarea. Is there any way by which this can be done because the textarea field is then stored into a mysql database and thus i have to limit the user data according to the varchar value of the field.
我在 html 上制作的表单中使用 aa textarea 字段,处理由 php 完成。我想限制用户可以在 textarea 中输入的字符数。有什么方法可以做到这一点,因为 textarea 字段然后存储到 mysql 数据库中,因此我必须根据该字段的 varchar 值限制用户数据。
Any help?
有什么帮助吗?
回答by s.webbandit
You can limit this by setting maxlengthattribute on textarea tag like <textarea maxlength="120"></textarea>in HTML and additionally "cut" input string in PHP by substr()function.
您可以通过maxlength像<textarea maxlength="120"></textarea>HTML一样在textarea 标签上设置属性来限制这一点,另外还可以通过substr()函数在 PHP 中“剪切”输入字符串。
回答by toni_lehtimaki
As mentioned by s.webbandityou can use maxlength, but note that this is a new feature introduced in HTML5, as W3 Schools articleabout <textarea>, so "bulletproof" solution needs Javascript. So maxlengthis notsupported in Internet Explorer until version 10 and in Opera not at all.
正如s.webbandit所提到的,您可以使用maxlength,但请注意,这是 HTML5 中引入的一项新功能,如 W3 Schools文章关于<textarea>,因此“防弹”解决方案需要 Javascript。所以直到版本 10 才在 Internet Explorer 中受支持,而在 Opera 中maxlength则完全不支持。
Here is one approach that uses Javascript to help you out:
这是一种使用 Javascript 来帮助您的方法:
<script language="javascript" type="text/javascript">
function charLimit(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
} else {
limitCount.value = limitNum - limitField.value.length;
}
}
</script>
You can then use it like this (original source):
然后您可以像这样使用它(原始来源):
<textarea name="limitedtextarea" onKeyDown="charLimit(this.form.limitedtextarea,this.form.countdown,100);">
</textarea>
This should get you going.
这应该能让你继续前进。
回答by JoBaxter
This is a very easy way to limit the length of a text area via JavaScript.
这是通过 JavaScript 限制文本区域长度的一种非常简单的方法。
text area field:
文本区域字段:
<input class='textField' type='text' NAME='note1' value='' onkeyup='charLimit(this, 40);'>
JavaScript function:
JavaScript 函数:
function charLimit(limitField, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);}
}
When the user types past 40 characters it just removes them.
当用户输入超过 40 个字符时,它只会删除它们。
回答by jasonflaherty
I've tried a lot of different ways to limit text in textareas, and the best I have found (client side) is with the jQuery.Maxlength.js plugin. Simple quick and covers them all!
我尝试了很多不同的方法来限制 textareas 中的文本,我发现的最好的(客户端)是使用 jQuery.Maxlength.js 插件。简单快速,涵盖所有!
Step 1. Include Maxlength plugin script
步骤 1. 包含 Maxlength 插件脚本
<script language="javascript" src="jquery.maxlength.js"></script>
Step 2. Add maxlength attribute to
步骤 2. 将 maxlength 属性添加到
<textarea maxlength="35"
rows="5" cols="30" name="address"></textarea>
Step 3. Initialize plugin
步骤 3. 初始化插件
<script type="text/javascript">
$(document).ready(function($) {
//Set maxlength of all the textarea (call plugin)
$().maxlength();
})
</script>
回答by ufuk
<script type="text/javascript">
$(document).ready(function(){
var maxChars = $("#sessionNumtext");
var max_length = maxChars.attr('maxlength');
if (max_length > 0) {
maxChars.bind('keyup', function(e){
length = new Number(maxChars.val().length);
counter = max_length-length;
$("#sessionNum_counter").text(counter);
});
}
});
</script>
<textarea name="sessionNumtext" id="sessionNumtext" maxlength="20" type="text"></textarea>
kalan karakter say?s?: <span id="sessionNum_counters">20</span>

