|
From: Lasse Reichstein Nielsen on 7 May 2008 01:12 "teser3(a)hotmail.com" <teser3(a)hotmail.com> writes: > I have the below that limits the textarea input to 500 characters but > cant get the alert message to work. It doesnt show anything. Do you get any error messages from the browser? Does the line previous to the alert work? I.e., is the textarea contents restricted to 500 chars? > alert("You are trying to enter more > than 500 characters"); The linebreak is not allowed in a string. If this is your actual code, that could be the problem. /L -- Lasse Reichstein Nielsen - lrn(a)hotpop.com DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'
From: Evertjan. on 7 May 2008 03:54 teser3(a)hotmail.com wrote on 07 mei 2008 in comp.lang.javascript: > > I have the below that limits the textarea input to 500 characters but > cant get the alert message to work. It doesnt show anything. Please > advise. > <script language="javascript" type="text/javascript"> > function limitText(limitField, limitCount, limitNum) { > if (limitField.value.length > limitNum) { > limitField.value = limitField.value.substring(0, limitNum); > alert("You are trying to enter more > than 500 characters"); beware of linebraks!!! > } else { the else is not needed. > limitCount.value = limitNum - limitField.value.length; > } >} > </script> > ----------------------------------------------------------------------- > --------- > > <form name="myform"> > <textarea name="limitedtextarea" > onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown, > 500);" onKeyDown="limitText(this,this.form.countdown, 500);" does the same. [If you are John, saying "the son of my father named John" is in effect the same as saying "me".] The onkeyup in fact is all that is needed. > onKeyUp="limitText(this.form.limitedtextarea,this.form.countdown, > 500);"> > </textarea> This works fine: ================== <script type='text/javascript'> var max = 500; // or debug with 5 function limitText(limitField, limitCount, limitNum) { if (limitField.value.length > limitNum) { limitField.value = limitField.value.substring(0, limitNum); alert('Do not enter more than '+ limitNum +' characters'); }; limitCount.value = limitNum - limitField.value.length; }; </script> <form name='myform'> <textarea name='limitedtextarea' onKeyUp='limitText(this,this.form.countdown,max);'> </textarea> <br><br><input name='countdown' readonly> </form> =================== Well, not perfect, if you add letters in the middle, another letter will be deleted, the one at the end. And not perfect, because you can add a long sentence by pasting [ctrl-V] So additional tests have to be done, preferably onsubmit [and also serverside testing, if Javascript is switched off, or if the clientside code is manipulated.] Depending on the OS, a <return> will count for two, or one character. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
From: Evertjan. on 7 May 2008 04:17 Evertjan. wrote on 07 mei 2008 in comp.lang.javascript: > This works fine: > > ================== > <script type='text/javascript'> > var max = 500; // or debug with 5 > function limitText(limitField, limitCount, limitNum) { > if (limitField.value.length > limitNum) { > limitField.value = limitField.value.substring(0, limitNum); > alert('Do not enter more than '+ limitNum +' characters'); > }; > limitCount.value = limitNum - limitField.value.length; >}; > </script> > > <form name='myform'> > <textarea name='limitedtextarea' > onKeyUp='limitText(this,this.form.countdown,max);'> > </textarea> > <br><br><input name='countdown' readonly> > </form> > =================== > > Well, not perfect, if you add letters in the middle, another letter > will be deleted, the one at the end. > > And not perfect, because you can add a long sentence by pasting > [ctrl-V] > > So additional tests have to be done, preferably onsubmit [and also > serverside testing, if Javascript is switched off, or if the > clientside code is manipulated.] > Try this, IE tested, the oncontextmenu seems IE specific: ================ <script type='text/javascript'> var max =10; function limitText(limitField, limitCount, limitNum) { if (limitField.value.length > limitNum) { limitField.value = limitField.old; alert('Do not enter more than '+ limitNum +' characters'); }; limitCount.value = limitNum - limitField.value.length; limitField.old = limitField.value; }; </script> <form name = 'myform'> <textarea name = 'limitedtextarea' onkeydown = 'this.old=this.value;' onKeyUp = 'limitText(this,this.form.countdown,max);' oncontextmenu = 'alert("No pasting please");return false;'> </textarea> <br><br><input name='countdown' readonly> </form> ================ Ctrl-V is only allowed if the result is within limit. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
|
Pages: 1 Prev: JavaScript A* Path Finding Prototype Next: Hope this will help u |