检查多行文本框大小 [英] Check size of multi-line textbox
问题描述
我一直在尝试了几个小时摸不着头脑。
为什么要在一个多行文本框的ASP控制,我有140个字符的限制,但有时我可以输入141或142个字符,特别是使用回车键?
下面是我的code
函数CheckSize(0,大小){
如果(O!= NULL){
变种S = o.value.length;
如果(S>大小){
调试器;
警报('。Lamentamos MAS FOI atingidoØLIMITE段的MáximoØtexto一个introduzir \\ nSugerimos阙introduza嗯texto COM menos德'+规模+'CARACTERES \\ nAgradecemos一个SUA COM $ P $这Pensão。');
//警报(resources.error_reachedMaxNumberOfChars.replace({0},字符串(大小)));
变种修剪= o.value.trim();
o.value = trim.substring(0,大小 - 1);
}
}
}
我的猜测是,是,你正在运行到相当普遍的最大长度
的错误,已经$ P在众多现代浏览器$ psent(<一个href=\"https://www.google.se/search?q=textarea%20maxlength%20bug&oq=textarea%20max&aqs=chrome.0.59l2j0j57j62l2.6501j0&sourceid=chrome&ie=UTF-8\"相对=nofollow>做一个谷歌搜索一下)。这是你如何可以解决此问题的一个例子,虽然未硬化的每一种情况(即粘贴)。
CSS
.hidden {
visibility:hidden的;
}
HTML
&LT; textarea的ID =myTextboxMAXLENGTH =140行=4COLS =35&GT;&LT; / textarea的&GT;
&LT; DIV ID =计数&GT; 0℃; / DIV&GT;
&LT; DIV ID =警戒级=隐藏&gt;对于最多字符&LT;!/ DIV&GT;
&LT; DIV ID =换行级=隐藏&GT;新行prevented,因为它是2个字符&LT;!/ DIV&GT;
的JavaScript
VAR myTextBox =的document.getElementById('myTextbox'),
数=的document.getElementById(计数),
警报=的document.getElementById('警报'),
换行符=的document.getElementById('新行'),
最大长度= 140;myTextBox.addEventListener(输入,功能(E){
VAR值= e.target.value.replace(/ \\ r吗?\\ n /克,'\\ r \\ n),
长度= value.length;
如果(长度GT; =最大长度){
如果(长度GT;最大长度){
长度=最大长度 - 1;
e.target.value = value.slice(0,长度);
newline.classList.remove('隐藏');
}其他{
alert.classList.remove('隐藏');
newline.classList.add('隐藏');
}
}其他{
alert.classList.add('隐藏');
newline.classList.add('隐藏');
} count.textContent =长度;
},FALSE);
I've been trying for hours to figure this out. Why in a multi-line textbox asp control, I have a limit of 140 characters but sometimes I can input 141 or 142 characters, especially with the use of the enter key?
Here's my code
function CheckSize(o, size) {
if (o != null) {
var s = o.value.length;
if (s > size) {
debugger;
alert('Lamentamos mas foi atingido o limite máximo para o texto a introduzir.\nSugerimos que introduza um texto com menos de ' + size + ' caracteres.\nAgradecemos a sua compreensão.');
// alert(resources.error_reachedMaxNumberOfChars.replace("{0}",String(size)));
var trim = o.value.trim();
o.value = trim.substring(0, size - 1);
}
}
}
My guess is, is that you are running into the fairly common maxLength
bug that has been present in a number of modern browsers (do a Google search about it).This is an example of how you could work around the problem, though not hardened for every situation (i.e. paste).
CSS
.hidden {
visibility: hidden;
}
HTML
<textarea id="myTextbox" maxlength="140" rows="4" cols="35"></textarea>
<div id="count">0</div>
<div id="alert" class="hidden">At max chars!</div>
<div id="newline" class="hidden">Newline prevented as it is 2 characters!</div>
Javascript
var myTextBox = document.getElementById('myTextbox'),
count = document.getElementById('count'),
alert = document.getElementById('alert'),
newline = document.getElementById('newline'),
maxLength = 140;
myTextBox.addEventListener('input', function (e) {
var value = e.target.value.replace(/\r?\n/g, '\r\n'),
length = value.length;
if (length >= maxLength) {
if (length > maxLength) {
length = maxLength - 1;
e.target.value = value.slice(0, length);
newline.classList.remove('hidden');
} else {
alert.classList.remove('hidden');
newline.classList.add('hidden');
}
} else {
alert.classList.add('hidden');
newline.classList.add('hidden');
}
count.textContent = length;
}, false);
On jsFiddle
这篇关于检查多行文本框大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!