PDA

View Full Version : Cancelling events


dtremblay
06-15-2006, 05:07 PM
Hi,

I would like to cancel a keyboard event, or achieve a similar result. For example, when entering numerical data, I would like to enforce that only one '.' can be entered. So, on the keydown event, I would check if a '.' is already present, and if so ignore it.

Any idea how I'd fix my code to do this?

Thanks.


<edittext resizable="false" fontsize="15" width="40" pattern="[0-9.]*" maxlength="3" text="$path{'@week'}" fontstyle="bold">
<handler name="onkeydown" args="k">
var val = this.getText().indexOf(".");
Debug.write("text:" + this.getText() + ", val:" + val);
if (val>-1){
Debug.write("Period is present");
return false;
}else{
Debug.write("Period is NOT present");
return true;
}
</handler>
</edittext>

paou
06-15-2006, 11:34 PM
if it was me then I'd approach it slightly differently,

I would overide the setText method, and then change the string in code so that although the characters get enetered, if they're invalid, they never get added to the string.

For example , inside the edittext

<method name="setText" args="t"> <![CDATA[

var newString;

// Copy only legal chars to newString from t

super.setText(newString);
]]></method>

dtremblay
06-16-2006, 05:16 AM
This is a great idea. I'll try this. It sounds like it's going to work. I'm happy now.

Dan