[Closed] Check a EditText box for numbers ???
I like to check my MyEditText.text if it has numbers or letters…
if it has numbers, I like it to return the number as a integer (xxx as integer
But if it has letters, I want it to pop up a messagebox “You may only type numbers, try again”
How do I do that ?
Thankx
This is all I could come up with. Run the text through a function like the one below. If it returns false, you’ve got non-numeric characters in there.
fn isNumeric text =
(
numeric_chars = #("0","1","2","3","4","5","6","7","8","9")
ss = stringStream text
while not eof ss do
(
char = readChar ss
if (findItem numeric_chars char) == 0 do return false
)
return true
)
It looks like a horrible hack, but it works :hmm:
RH
(
rollout test "Test"
(
edittext myEditText text:""
label theLabel "Ready."
on myEditText changed txt do
(
try
(
val = execute txt
theLabel.text = "You typed in the integer "+ (val as integer) as string
)catch
theLabel.text = "Please type in only numbers!"
)
)
createDialog test 300 60
)
The cool thing is that you can even type in expressions, for example if you enter
Pi*2
the result reported will be 6 and not “Please type in only numbers”
You can also type in 2*5+10 – it will report “You typed in the integer 20”!
Cheers,
Bobo
That’s a very elegant solution, Bobo! I figured there was probably something better than what I offered, but I couldn’t think of what it might be.
RH