Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

i have created one form for user to submit a abstract. but while submitting i need to check weather they have added more than 250 words or not. i need to allow only 250 words . how to do this ?

i have tried some JavaScript but it works for only 250 character.

here is my code

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);
      }

and my textarea is :

  <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>

how to limit for 250 words .

thanks in advance

like image 875
Nayana Avatar asked Apr 03 '15 06:04

Nayana


People also ask

How do I limit the number of words in a textarea?

The HTML <Textarea>maxlength attribute is used to specify the maximum number of characters enters into the Textarea element. Attribute Value: number: It contains single value number which allows the maximum number of character in Textarea element.

How do I limit the input characters in a text field?

To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively. To limit the number of characters, use the maxlength attribute.

What is the way to keep users from typing text into a large text area?

What is the way to keep users from typing text into a large text area? For input type="text" , we can use the size attribute to specify the visible size of the field, in characters. But we can also use the maxlength attribute to specify the maximum amount of characters that can be entered.

Which attribute is used to limit the text to be given in the textarea?

The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into an <input> or <textarea> . This must be an integer value 0 or higher. If no maxlength is specified, or an invalid value is specified, the input or textarea has no maximum length.


2 Answers

To prevent submitting form when there is more than 250 characters in the textarea, add id="box_id" to your textarea and add this event to form element:

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

Now in your function split this value by multiple spaces:

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;
    }
}

To take advantage from new HTML attributes, you can also add "pattern" attribute to textarea with regex limiting input to 250 words:

<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>

This regex pattern was taken from following SO thread, which touches similar problem with 250 words: Limit the number of words in a response with a regular expression

like image 69
n-dru Avatar answered Oct 21 '22 14:10

n-dru


use this:

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);
    }
}
like image 26
Shoaib Shakeel Avatar answered Oct 21 '22 14:10

Shoaib Shakeel