如何限制用户在文本区域只能输入250个字?

how to restrict the user to enter only 250 words in text area?

我已经创建了一个表格供用户提交摘要。但是在提交时我需要检查天气他们是否添加了超过 250 个单词。我只需要允许 250 个字。怎么做?

我已经尝试了一些 JavaScript 但它只适用于 250 个字符。

这是我的代码

function maxlength(element, maxvalue)
   {
  var q = eval("document.upload."+element+".value.length");
   var r = q - maxvalue;
   var msg = "Sorry, you have input "+q+" characters into the "+
  "text area box you just completed. It can return no more than "+
  maxvalue+" words to be processed. Please abbreviate "+
 "your text by at least "+r+" words";
    if (q > maxvalue) alert(msg);
      }

我的文本区域是:

  <tr>
   <td><label>Abstract Details</label> </td>
 <td><label>Please enter data, at most 250 words:</label>
  <br/><textarea rows="6" cols="80"name="box_name"   onChange="maxlength('box_name', 250)" style="width: 257px; height: 131px;">    </textarea></td>
    </tr>
  <tr>

如何限制 250 个字。

提前致谢

如果你想要250个字符,你可以将maxlength属性设置为250

如果你想要 250 个字:

function maxlength(obj,wordLen){
   var len = obj.value.split(/[\s]+/);
   if(len.length > wordLen){
       alert("You cannot put more than "+wordLen+" words in this text area.");
   }
 }

函数调用:

onChange="maxlength(this, 250)"

Working Demo

要防止在 textarea 中超过 250 个字符时提交表单,请将 id="box_id" 添加到您的 textarea 并将此事件添加到表单元素:

onsubmit="return maxlength(getElementById('box_id'), 250);

现在在你的函数中用多个空格分割这个值:

function maxlength(element, maxvalue){
    var q = element.value.split(/[\s]+/).length;
    if(q > maxvalue){
        var r = q - maxvalue;
        alert("Sorry, you have input "+q+" words into the "+
        "text area box you just completed. It can return no more than "+
        maxvalue+" words to be processed. Please abbreviate "+
        "your text by at least "+r+" words");
        return false;
    }
}

要利用新的 HTML 属性,您还可以将 "pattern" 属性添加到文本区域,使用正则表达式将输入限制为 250 个字:

<textarea rows="6" cols="80"name="box_name"   onChange="maxlength(this, 250)" style="width: 257px; height: 131px;" 
    pattern="^(?:\b\w+\b[\s\r\n]*){1,250}$">
</textarea>

此正则表达式模式取自以下 SO 线程,该线程涉及 250 个单词的类似问题:Limit the number of words in a response with a regular expression

使用这个:

function maxlength(element, maxvalue)
{
    var value = $(elment).val();
    var words = value.split(' ');

    var msg = 'Sorry, you have input ' + words.length + ' words into the ' +
        'text area you just completed. It can return no more than ' +
        maxvalue + ' words to be processed. Please abbreviate ' +
        'your text by at least ' + (words.length - maxvalue) + ' words';

    if (words.length > maxvalue) {
        alert(msg);
    }
}

这条语句eval("document.upload." + element + ".value.length")将return没有characters而不是words,如果你想限制250 words那么就拆分value of textarea使用 space 并使用 max words allowed 进行验证,然后使用 return 适当的消息:

您可以试试这个代码。

function maxlength(element, maxvalue) {
    var q = eval("document.upload." + element + ".value");
    var textLength = q.split(/\S+/).length - 1
    var r = textLength - maxvalue;
    var msg = "Sorry, you have input " + textLength + " characters into the " +
        "text area box you just completed. It can return no more than " + maxvalue + " words to be processed. Please abbreviate " +
        "your text by at least " + r + " words";
    if (textLength > maxvalue) alert(msg);
}