PDA

View Full Version : Limiting size of inputtext field


emaender
02-17-2004, 07:26 AM
Hello!

We are working on an application that will interface with a back end data system. The database for this system has limits on the writable fields we will access, therefore it is necessary for us to somehow limit the length of user input in the input text field.

I tried the following code just to see if I could dynamically access the length of the inputtext at runtime:

<canvas>
<class name="mytext" width="150" height="20" bgcolor="gray">
<attribute name="text" type="text"/>
<inputtext resizable="false" text="${parent.text}" bgcolor="white"
x="1" y="1"
width="$once{parent.width-2}" height="$once{parent.height-2}">
<method event="ontext">
debug.write("Text width: " + this.getTextWidth() );
</method>
</inputtext>
</class>
<mytext>This text is editable.</mytext>
</canvas>

However, in the debug window I am getting "Text width: 1" everytime I type in the inputtext field, so I know that the "this.getTextWidth" is not working for me.

Can you help me with this? Any alternate approach would be fine as well. Thanks in advance for your help here!

emaender
02-17-2004, 08:04 AM
OK.

I'm able to use the getSelectionPosition() method to find out the position of my cursor, and therefore determine if the user has typed beyond a pre-defined limit.

Now I'm struggling to figure out how to stop the user from typing further. I mean, I could pop up a modal dialog everytime they type beyond my limit, but that still doesn't stop them from continuing to type one character at a time.

Ideally, I'd like to somehow mimic a hard stop, where if the user reaches the end of the field, further typing will have no effect on the input text field. Any thoughts on how I can accomplish this?

Thanks again!

antun
02-17-2004, 09:03 AM
You could try something like:


<method event="ontext">
var maxTextLength = 30;
if ( this.text.length >= maxTextLength ) {
// code that pops off last character,
// overwrites text with a previously saved
// variable, etc.
}
</method>


Does that help at all?

-Antun

emaender
02-17-2004, 09:18 AM
Yes, I was able to implement a solution exactly as you recommended. Thanks!

- Eric