Page 1 of 1

Testing a field for valid number

Posted: Thu Apr 10, 2014 2:37 pm
by trags3
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

Re: Testing a field for valid number

Posted: Thu Apr 10, 2014 3:09 pm
by FourthWorld
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"

Re: Testing a field for valid number

Posted: Thu Apr 10, 2014 3:11 pm
by Klaus
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

Re: Testing a field for valid number

Posted: Thu Apr 10, 2014 3:37 pm
by trags3
Richard & Klaus
Thanks a lot. That works fine.
Tom

Re: Testing a field for valid number

Posted: Thu Apr 10, 2014 10:32 pm
by Simon
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