Testing a field for valid number

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
trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Testing a field for valid number

Post by trags3 » Thu Apr 10, 2014 2:37 pm

i have 2 test input boxes and a button on a card.
field price
field ans
a button has this code

on mouseUp
if price is an integer
put "Yes" into ans
else
put "no" into ans
endif
end mouseUp

when I click the button nothing happens

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Testing a field for valid number

Post by FourthWorld » Thu Apr 10, 2014 3:09 pm

In your example code "ans" isn't accompanied by an object reference, and so is being treated like a variable.

To put a value into a field, include the "field" object specifier, e.g.:

put "SomeValue" into field "ans"
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Testing a field for valid number

Post by Klaus » Thu Apr 10, 2014 3:11 pm

Hi trags3,

you are using price and ans like variables, but tehy are in fact fields!
So you need to tell LC this fact :D

Code: Select all

on mouseUp
  if fld "price" is an integer then
     put "Yes" into fld "ans"
  else 
     put "no" into fld "ans"

  ## END IF are 2 words! 
  ## Wrong: endif
  end if
end mouseUp
Best

Klaus

trags3
Posts: 432
Joined: Wed Apr 09, 2014 1:58 am

Re: Testing a field for valid number

Post by trags3 » Thu Apr 10, 2014 3:37 pm

Richard & Klaus
Thanks a lot. That works fine.
Tom

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Testing a field for valid number

Post by Simon » Thu Apr 10, 2014 10:32 pm

Hi Tom,
From the users side of things you should be checking while they enter characters into the field not after.
In the field script

Code: Select all

on keyDown tkey
   if tKey is not a number and tKey is not in ".$" then
         answer "Please enter numbers only"
      else
          pass keyDown
      end if
end keyDown
That will allow for decimal points and "$" but not letters. Make sure in the fields inspector you have "tab on return" selected.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply