TextField question

Hi everyone
I’m developing a single application with a TextField that does’t not allow characters only numbers. How I do to prevent the Textfield from Strings Insertion either TextChange method of the item or with a button? I appreciate any help

Sebastian

This is demonstrated on numerous topics thru out this forum

Basically put code in KEYDOWN action for the text field that returns TRUE or FALSE based on the characters you wish to “accept”

OR use the MASK property

See http://documentation.xojo.com/index.php/TextEdit.Mask

The Mask does not seem to prevent non-numerics from being poked into the textfield. With a Mask of either “#########” or “999999999”, another control could still do TextField1.Text = "Non-numeric Letters". And a non-nmeric value can still be pasted into the field. To be honest, I’m not sure how to get around this.

Edit: amending my post for “value can still be pasted into the field”. Use the TextChange event to capture that scenario.

Bottom line: use a mask or the KeyDown event to filter input, with a final sanity check in TextChange.

Well, I was going to create you something to show an example; however, it appears isNumeric DOES NOT WORK with text objects. Damn it, Xojo… I’m trippin’ over all the bugs over here. Starting to get aggravated, once again.

From what I see on Mac, Mask prevents pasting alphanumeric characters into the TF.

Messing up the content with direct modification of the Text property will always be possible, but quite frankly, I feel a programmer should be adult enough to prevent such an occurrence.

However, since one can never know how powerful the Universe will create a new idiot, here is the best I can come up with :

Function onlyNumbers(S as String) As String If S = "" then return "" If not IsNumeric(S) then for i as integer = 32 to 255 if i < 48 or i > 57 then if instr(S,chr(i)) > 0 then S = ReplaceAll(S,chr(i),"") end if end if next end if Return S End Function

In Textfield :

Me.Text = onlyNumbers(me.Text)

PS : Note that for performance concerns, I limited the range of characters to 32-255. But if you fear the user may enter Arabic, for instance, you may go up to 3520. Or 8000 or so for Asian ideograph. A quick test shows it still works, though.

What do you mean ?

This works perfectly :

system.debuglog str(isnumeric("tada")) // Shows False system.debuglog str(isnumeric("1234")) // Shows True

[code] dim textValue as text

textValue = “4”

if isNumeric(textValue) then
print “textValue is numeric”
else
print “textValue is NOT numeric”
end if[/code]

This will return as NOT numeric…

[quote=199280:@Eric Brown][code] dim textValue as text

textValue = “4”

if isNumeric(textValue) then
print “textValue is numeric”
else
print “textValue is NOT numeric”
end if[/code]

This will return as NOT numeric…[/quote]

I was just experimenting and found the same. That new framework is still green. You should file a bug report.

Until that’s fixed :

[code] dim t as Text = “2345”
dim textValue as string = t

if isNumeric(textValue) then
msgbox “textValue is numeric”
else
msgbox “textValue is NOT numeric”
end if[/code]

can you EXTEND the Text datatype and cast it to STRING?

isNumeric(String(t))

[quote=199282:@Dave S]can you EXTEND the Text datatype and cast it to STRING?

isNumeric(String(t))[/quote]

You can… which is what I will have to do until this gets resolved.

pretty soon XOJO will be like SWIFT in the respect that you have to CAST everything…if the datatypes don’t match EXACTLY

Better : Overload so it fixes the bug :

Function isNumeric(T as Text) As Boolean dim s as string = T return isNumeric(S) End Function

Just tested fine.