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 pleted. 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
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 pleted. 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
Share Improve this question asked Apr 3, 2015 at 6:54 NayanaNayana 7258 silver badges27 bronze badges 4-
So you would like to allow
250 words
not `characters ? – Mox Shah Commented Apr 3, 2015 at 6:57 - Take a look to given link, Answer is available : stackoverflow./questions/16395785/… – Bhargav Bhanderi Commented Apr 3, 2015 at 6:58
-
1
Just split with
textarea
's value withspace
and verify thelength
withmaxlength
– Mox Shah Commented Apr 3, 2015 at 7:00 - 3 stackoverflow./questions/17909646/… here you go first link from google when you search for it. – valar morghulis Commented Apr 3, 2015 at 7:03
4 Answers
Reset to default 9To 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 pleted. 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 pleted. 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 want 250 characters, You can set the maxlength attribute to 250
if you want 250 words:
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.");
}
}
Function Call:
onChange="maxlength(this, 250)"
Working Demo
This statement eval("document.upload." + element + ".value.length")
will return no of characters
not words
, if you want to limit 250 words
then just split value of textarea
with space
and verify it with max words allowed
and then return appropriate message:
You may try this code.
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 pleted. 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);
}