Page 1 of 1

Max characters indicator in text area

Posted: Wed Sep 05, 2012 4:25 pm
by Patrik iden
Put this code in the HTML of text area (Inside Tag*)

Code: Select all

onKeyDown="textCounter(this,'progressbar1',500)" 
onKeyUp="textCounter(this,'progressbar1',500)" 
onFocus="textCounter(this,'progressbar1',500)"></textarea><br />

<div id="progressbar1" class="progress" style="position:absolute;left:15px;top:530px"></div>

<script>textCounter(document.getElementById("TextArea1"),"progressbar1",500)</script
Change 500 to how many characters you want to allow.
And change the ("TextArea1") to what you need.
And chane left:15px;top:530px to what you need.

Put this code in an html box (<head></head>tags)

Code: Select all

<style type="text/css">

.progress{
	width: 1px;
	height: 14px;
	color: white;
	font-size: 12px;
  overflow: hidden;
	background-color: #FF0000;
	padding-left: 5px;
       
}

</style>

<script type="text/JavaScript">

function textCounter(field,counter,maxlimit,linecounter) {
	// text width//
	var fieldWidth =  parseInt(field.offsetWidth);
	var charcnt = field.value.length;        

	// trim the extra text
	if (charcnt > maxlimit) { 
		field.value = field.value.substring(0, maxlimit);
	}

	else { 
	// progress bar percentage
	var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
	document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/101)+"px";
	document.getElementById(counter).innerHTML=" "+percentage+"%"
	// color correction on style from CCFFF -> CC0000
	setcolor(document.getElementById(counter),percentage,"background-color");
	}
}

function setcolor(obj,percentage,prop){
	obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}

</script>
Example page:
http://test3.fcab.se/members/test/maxcharacters.html

Re: Max characters indicator in text area

Posted: Wed Sep 05, 2012 6:16 pm
by [RZ]
this code can easily be fooled...
just mark text, copy and change the focus to your text area, then right click and select paste all times you want... so, no text limit nor progress bar working as expected...
sorry.

Re: Max characters indicator in text area

Posted: Wed Sep 05, 2012 6:31 pm
by [RZ]
it seems you also forgot something...

http://www.dynamicdrive.com/dynamicinde ... input2.htm
http://www.dynamicdrive.com/notice.htm

Form field Progress Bar
Author: Ron Jonk

Form field Limiter v2.0
Author: Dynamic Drive

why not mention the original authors/coders?

/***********************************************
* Form Field Progress Bar- By Ron Jonk- http://www.euronet.nl/~jonkr/
* Modified by Dynamic Drive for minor changes
* Script featured/ available at Dynamic Drive- http://www.dynamicdrive.com
* Please keep this notice intact
***********************************************/

Re: Max characters indicator in text area

Posted: Thu Sep 06, 2012 4:01 pm
by Patrik iden
[RZ] wrote:this code can easily be fooled...
just mark text, copy and change the focus to your text area, then right click and select paste all times you want... so, no text limit nor progress bar working as expected...
sorry.
Well, it works for me.

Re: Max characters indicator in text area

Posted: Thu Sep 06, 2012 4:03 pm
by Patrik iden
[RZ] wrote:it seems you also forgot something...

http://www.dynamicdrive.com/dynamicinde ... input2.htm
http://www.dynamicdrive.com/notice.htm

Form field Progress Bar
Author: Ron Jonk

Form field Limiter v2.0
Author: Dynamic Drive

why not mention the original authors/coders?

/***********************************************
* Form Field Progress Bar- By Ron Jonk- http://www.euronet.nl/~jonkr/
* Modified by Dynamic Drive for minor changes
* Script featured/ available at Dynamic Drive- http://www.dynamicdrive.com
* Please keep this notice intact
***********************************************/
Should have done this, sorry.

Re: Max characters indicator in text area

Posted: Thu Sep 06, 2012 5:16 pm
by [RZ]
Well, it works for me.
if you follow step by step what i told you, you will see -crystal clear- how easy is to "break" this script.
ok, don't worry, it's up to the end user to take or not this snippet. just to take in count the downside...