Only allow digits in dialog box field...

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
tjo.mobile
Posts: 18
Joined: Tue Oct 25, 2016 8:09 pm

Only allow digits in dialog box field...

Post by tjo.mobile » Wed Nov 02, 2016 9:00 am

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.

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

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

Post by MaxV » Wed Nov 02, 2016 12:05 pm

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#####
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

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

Post by [-hh] » Wed Nov 02, 2016 3:08 pm

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 ...
shiftLock happens

tjo.mobile
Posts: 18
Joined: Tue Oct 25, 2016 8:09 pm

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

Post by tjo.mobile » Wed Nov 02, 2016 7:27 pm

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.

Post Reply