Using script in a number field to default the field to zero

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
mikemc
Posts: 60
Joined: Sun May 10, 2015 5:42 pm

Using script in a number field to default the field to zero

Post by mikemc » Thu May 21, 2015 11:04 pm

Part A I have a button on a card that manipulates numbers input by a user in four fields . When the button is engaged
I would like the script in the number field to check if the the field is blank if it is I would like
to enter a zero in that field. Part B. Does this kind of code go into the script for the field? If
it does not go in the number field script what kind of stuff does and can you run it from a button.

SparkOut
Posts: 2961
Joined: Sun Sep 23, 2007 4:58 pm

Re: Using script in a number field to default the field to z

Post by SparkOut » Thu May 21, 2015 11:39 pm

You can make a check in the field script when a user edits or leaves the field (see "closeField" and "exitField"). But you will have to verify in the button script as well, in case nobody made an entry at all (or at least set a default value on openCard for instance).
So check in the button script,

Code: Select all

put field "numberfield" into tNum
if tNum is empty then --also maybe check "is not a number" or other validation
  put 0 into tNum
  put 0 into field "numberfield"
end if
--do stuff with tNum

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

Re: Using script in a number field to default the field to z

Post by Klaus » Thu May 21, 2015 11:43 pm

Hi Mike,

A & B:
you can use a "closefield" handler in the field script:

Code: Select all

on closefield
   if me = EMPTY then
     put 0 into me
   end if
end closefield
Best

Klaus

mikemc
Posts: 60
Joined: Sun May 10, 2015 5:42 pm

Re: Using script in a number field to default the field to z

Post by mikemc » Fri May 22, 2015 12:37 am

Thanks both pieces of code work good with one small change using "" instead of the word empty. I don"t know why
but this seems to work better on repeated tries

mikemc
Posts: 60
Joined: Sun May 10, 2015 5:42 pm

Re: Using script in a number field to default the field to z

Post by mikemc » Fri May 22, 2015 12:54 am

Not sure but I guess "" and empty are the same thing. Anyway the problem i'm having now is if you
put a couple of blank spaces in the field the code will not change the field to 0 but instead leaves it blank
any ideas?

SparkOut
Posts: 2961
Joined: Sun Sep 23, 2007 4:58 pm

Re: Using script in a number field to default the field to z

Post by SparkOut » Fri May 22, 2015 12:56 am

As I mentioned, also check if the contents "is a number" or not.
You could also check if it "is an integer" or not.

Post Reply