Page 1 of 1

Only allow digits in dialog box field...

Posted: Wed Nov 02, 2016 9:00 am
by tjo.mobile
Is there a way to limit user input to digits only when they are presented with a dialog box?

I use this code to limit input to digits for a text field

Code: Select all

on keyDown pKey
   
   put cr & "1" & cr & "2" & cr & "3" & cr & "4" & cr & "5" & cr & "6" & cr & "7" & cr & "8" & cr & "9" & cr & "0" into tAllowed

   if pKey is not in tAllowed then
      
      answer "Entries can only contian NUMBERS" with "OK"
      
   else
      
      pass keyDown
      
   end if
   
end keyDown
Is there a similar way to limit user input on a dialog box?

Thanks for any suggestions or advice,

TJ.

Re: Only allow digits in dialog box field...

Posted: Wed Nov 02, 2016 12:05 pm
by MaxV
Hi TJ,
first of all your code can be simpler:
########CODE#######
on keyDown pKey
if pkey is not a number then
answer "Entries can only contian NUMBERS" with "OK"
else
pass keyDown
end if
end keyDown
#####END OF CODE#####

Now for a dialog box, you can use this:

########CODE#######
on mouseUp
asknumber
end mouseUp

on asknumber
ask "How old are you?"
put it into temp
if temp is a number then
#...
else
answer "You can type only numbers"
asknumber
end if
end asknumber
#####END OF CODE#####

Re: Only allow digits in dialog box field...

Posted: Wed Nov 02, 2016 3:08 pm
by [-hh]
Your question looks as if you are interested in non-negative integers only.
So a more specialized option could be

Code: Select all

if it is not an integer or it < 0 then ...

Re: Only allow digits in dialog box field...

Posted: Wed Nov 02, 2016 7:27 pm
by tjo.mobile
Thanks for the awesome information and code examples! Very much appreciated. I have not tried them yet, but I am sure that will work.

And now I know how to do it the easier/simpler/better way! (and exclude negative integers too!)

Thanks again,

TJ.