This topic is locked

Limit Character for TextArea

3/30/2009 1:54:45 PM
PHPRunner Tips and Tricks
A
acpan author

Just to share after making it work.
Description: I need a simple way to limit the text area field and display "Characters Left" for

text area field by ONLY editing the Add form in the visual editor, WITHOUT touching

commonfunctions.php and etc. as suggested by some other methods in the forum.
PHPR Version: 5.0 Build 766 (Should also work for all versions)
Step 1: In Visual Editor, Switch to Html mode for the Add Form.

Step 2: Add the codes below after </HERAD> and <BODY> Tags,

note it works without any {literal} tags for me.
[codebox]

<!-- Limit Character -->

<script type=text/javascript>

/ Form field Limiter script- By Dynamic Drive /

var ns6=document.getElementById&&!document.all

function restrictinput(maxlength,e,placeholder){

if (window.event&&event.srcElement.value.length>=maxlength)

return false

else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){

var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys

if (pressedkey.test(String.fromCharCode(e.which)))

e.stopPropagation()

}

}
function countlimit(maxlength,e,placeholder){

var theform=eval(placeholder)

var lengthleft=maxlength-theform.value.length

var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)

if (window.event||e.target&&e.target==eval(placeholder)){

if (lengthleft<0)

theform.value=theform.value.substring(0,maxlength)

placeholderobj.innerHTML=lengthleft

}

}
function displaylimit(thename, theid, thelimit){

var theform=theid!=""? document.getElementById(theid) : thename

var limit_text='<b><span id="'+theform.toString()+'">'+thelimit+'</span></b> characters remaining'

if (document.all||ns6)

document.write(limit_text)

if (document.all){

eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}

eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}

}

else if (ns6){

document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true);

document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true);

}

}

</SCRIPT>

<!-- Limit Character -->

[/codebox]
Step 3: Switch to WYSIWYG Mode, click on the TextArea Field, so that it's selected.

Switch to HTML Mode and you will notice the cursor is located at the TextArea Field.

and insert the following, where msg_text is my field name for the TextArea field:
[codebox]

<!-- Limit Character -->

<script> displaylimit("document.editform.value_msg_text","",160)</SCRIPT>

<!-- Limit Character -->

[/codebox]
Step 4: It will look like the following:
[codebox]

{BEGIN msg_text_fieldblock}<TR>

<TD class=editshade_b style="PADDING-LEFT: 15px" width=150>Msg Text</TD>

<TD class=editshade_lb style="PADDING-LEFT: 10px" width=250>{$msg_text_editcontrol}

&nbsp;<IMG src="images/icon_required.gif">

<!-- Limit Character -->

<script> displaylimit("document.editform.value_msg_text","",160)</SCRIPT>

<!-- Limit Character -->

</TD></TR>

{END msg_text_fieldblock}

[/codebox]
Step 5: Generate the project and test it. You should see "XX characters remaining"

below your TextArea field and it will be counting down in real-time when you type on the field.

There is nothing to be changed at the codes on step 2, just step 3, where you put in

the right field name.
P.S. this code is adapted from another post here

and i have tested working and clean it up and put down the specific working steps

if it helps other.

A
acpan author 5/30/2010

Hi
Any idea how to adapt this to phpr 5.2 ? It's not working when i use it in 5.2.
Thanks

acp

A
acpan author 6/1/2010

To support 5.2, replace the
<!-- Limit Character -->

<script> displaylimit("document.editform.value_msg_text","",160)</SCRIPT>

<!-- Limit Character -->
with this:
<!-- Limit Character -->

<script> displaylimit("document.editform1.value_msg_text_1","",160)</SCRIPT>

<!-- Limit Character -->

K
kausi 1/12/2011

Does anybody have an idea how to get the code working on 5.3? worked fine on 5.2 but not 5.3
Thanks

KS

K
kausi 1/12/2011



Check this:

http://www.asprunner.com/forums/topic/14819-how-to-limit-number-of-characters-in-the-text-area/


Thanks for quick answer. Unfortunatelly cannot use linked script as it is working with only one text area per page. I have a page containg multiple text areas.
KS

Admin 1/12/2011

I guess you need to repeat this code for each text area on the page (replacing the field name of course).

K
kausi 1/13/2011



I guess you need to repeat this code for each text area on the page (replacing the field name of course).



Could not het the code working as well. Run it on chrome analyse tool and it says "Uncaught TypeError: Object false has no method 'getValue'"
My OnPageLoad event is as follows

var ctrlFieldName = Runner.getControl(pageid, 'TempAction_1');

document.getElementById('counter').value = ctrlFieldName.getValue().length;
function func()

{

//update counter

document.getElementById('counter').value = ctrlFieldName.getValue().length;
//check if length of the entered value <40 characters

if (ctrlFieldName.getValue().length>40)

{

//show first 40 characters only

ctrlFieldName.setValue(ctrlFieldName.getValue().substr(0,40));

//update counter

document.getElementById('counter').value = 40;

}

}

ctrlFieldName.on('keyup', func);

I have added the code

<TD style="WIDTH: 122px">&nbsp;<STRONG>{$label OpenClaims TempAction}</STRONG></TD>

<TD style="WIDTH: 360px" colSpan=3>{$TempAction_editcontrol}&nbsp;

<INPUT id=counter disabled value=0 size=3> </TD></TR>

Admin 1/13/2011

Impossible to tell what might be wrong without seeing your files.
I would recommend to post your application to Demo Account and open a ticket at http://support.xlinesoft.com sending your Demo Account URL. 'Demo Account' button can be found on the last screen in the program.

J
jkeagle13 5/23/2011

Hello,
I am able to get this working, thank you VERY much for your help and guidance.
Two questions. First, I need to have this present on the edit page as well. What needs to be modified in order to make that work?
Second, I have multiple text boxes on the screen. Each needs to have its own distinct character counters. How much of the code do I need to change in order to get it to work? Can some of the functions be recalled, for example, or do I need to add a suffix to each function and variable?
Thank you very much!

Joseph Irvine