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
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.
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? 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.
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.
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
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With