How to test for a number

Klaus Major klaus at major-k.de
Wed Mar 16 10:36:34 EST 2005


Hi Kevin,

> How do you make an If check to check to see if a user has entered a
> number and not a letter?
> I can stop it from doing a divide by 0 doing this:
>
> on mouseUp
> put field"num1" into tAdd
> put field"num2" into tAdd2
>   if tAdd2 is 0 then
>     answer "You can't divide by 0"
>   else
>     put tAdd/tAdd2 into field"sum"
>     end if
> end mouseUp

well, check "if add2 is a number" :-)

on mouseUp
    put field"num1" into tAdd
    put field"num2" into tAdd2
    if tAdd2 is 0 then
       answer "You can't divide by 0"
       exit mouseup
    end if
    if tAdd2 is a number then
       put tAdd/tAdd2 into field"sum"
    else
       answer "*##+[¢]|­ÀÀÒ
       ## Or something more meaningful :-)
    end if
end mouseUp

But you can also restrict your field to only allow numbers!

Put this into the script of your field:

on keydown tKey
   if tKey is a number then
      pass keydown
   else
     beep
   end if
end keydown

This way only numbers can be entered into that field...

Hope that helps.

> Thanks
>
> Kevin

Regards

Klaus Major
klaus at major-k.de
http://www.major-k.de



More information about the use-livecode mailing list