Checking valid values

How can I check if a variable (typed in with the keyboard) is a valid value ?
I want to use Lua command ‘type’ but that is not included in Codea, why not ??
I made a function to check what is typed in, a value (0…9) or a minus sign or a dot or a backspace or return, all these are valid.
But (typed in by accident) two or more minis signs and/or dots are not valid → program will stop, all values typed in before are lost.

You can check my code for “unit converter” where I used this logic. This logic can be found in “Buttons” class. You can apply this logic in global function

function keyboard (key)
– write your logic here

You can learn more about this function from codea documentation

@hartland all keyboard values are given as strings. However if you would like to validate a string as a number, you can do it like this:

str is your string representation of the number.

local num = tonumber(str)

if num ~= nil then 
    -- str is a valid number
    -- it is now available in num
    -- str was not a valid number

Thanks Simeon, that works!

But, Simeon, I was earlier looking for Lua command ‘tonumber’, it is not mentioned in the reference key documentation (the eye) and it is not given in the auto completion.
Are there more Lua commands which are not mentioned in the reference key documentation and in the auto completion?

rashedlatif, also thanks.


I must have missed including that one. Will add it to the docs and autocomplete.

Hi Simeon, please do so.
Autocomplete is very usefull to check if a Lua command is used in Codea!

No problem, I’ve added these to autocomplete for the next update.