PDA

View Full Version : Limit an edit-box to Integer/float value.


DaveWortley
03-24-2009, 05:24 PM
How can i stop the user from typing characters into an edit-box? I need it to be limited to integers or sometimes float values.

harefort
03-24-2009, 06:44 PM
You could check it after the user entered something:

on edtText entered sString do
(
if classOf sString == integer or classOf sString == float then
(
--do stuff
)
else
(
print "Please enter a number!"
)
)

thatoneguy
03-24-2009, 07:22 PM
try (destroydialog RolloutBob)catch()
fn FilterNumbers t =
(
badchars = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/?>,<+=-_)([]{}*&^%$#@!~`;:'\"|\\"
WorkingString = ""
t = filterstring t badchars
for i=1 to t.count do WorkingString += t[i]
t = WorkingString
WorkingString = filterstring t "." splitEmptyTokens:true
if WorkingString.count > 2 do
(
t = ""
t += WorkingString[1]+"."
for i = 2 to WorkingString.count do t+= WorkingString[i]
)
return t
)
rollout RolloutBob "Bob" (
edittext textbox "Enter Some Text"

on textbox changed t do
(
textbox.text = filterNumbers t
)
)
createdialog RolloutBob

ZeBoxx2
03-24-2009, 08:10 PM
harefort - you'd have to cast the string to an integer/float first; and you then run into problems with certain time literals such as 't', 'f', etc. (i.e. ' "f" as integer ' will happily evaluate)

thatoneguy's (*waves*) solution is better, although I'd turn it around and filter only for known-allowed characters in this case (i.e. 0-9 and . or , depending on locale; best would be grabbing the decimal separator from the registry - HKEY_CURRENT_USER\Control Panel\International::sDecimal )

DaveWortley
03-24-2009, 08:24 PM
I could also do the obvious thing of using a spinner instead which I can limit to Float/Integer easily!

But thanks for the code anyway, I'll need it for some of them.


D

ZeBoxx2
03-24-2009, 09:37 PM
you could, but then you wouldn't be answering your own question ;)

besides... then you'll have to figure out how to hide the spinner controls - and keep them hidden. Unless it's meant to be a spinner in the first place :)

thatoneguy
03-24-2009, 11:02 PM
Is there an easy way to filter for only good characters?


fn goodstringchars stringvar goodchars =
(
for i = stringvar.count to 1 by -1 do (if not (matchpattern goodchars pattern:("*"+stringvar[i]+"*")) do stringvar = replace stringvar i 1 "")
stringvar
)
goodstringchars "3429acCld" "123456890."

ZeBoxx2
03-24-2009, 11:20 PM
other than using .NET - especially if this if for text entry into a text control anyway (as .NET gives you the character entered and you can parse that character specifically)? Not really.

Though I would go with findString in this case - simply because matchpattern will cause issues if the string to be checked has * or ? characters in it.

Note also that this isn't a full solution insofar as numbers goes. You'd have to check for period vs comma (as per the aforementioned), double-existence of them and allow the minus character (probably).

That's why a spinner -is- rather convenient, as it does all that for you.

martroyx
03-25-2009, 06:03 AM
"f" as integer ' will happily evaluate
good to know :thumbsup:

CGTalk Moderation
03-25-2009, 06:03 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.