Bram  Vanbilsen
Bram Vanbilsen

Reputation: 6505

Lua - Check if string contains valid characters

I'm trying to check whether or not all chars in my string are either one of these numbers: 1,2,3,4,5,6,7,8,9,0 OR a decimal point and whether they contain spaces or not. I'm using this piece of code but it's not working and I can't figure out why:

    print("Confirming added item")
    local validAmountString = true
    print(addScreen.amountTxtField.text)
    for i = 1, #addScreen.amountTxtField.text do
        local c = addScreen.amountTxtField.text:sub(i,i)
        print("Char " .. i .. " in the amount textflied is " .. c)
        if c == "0" or c == "1" or c == "2" or c == "3" or c == "4" or c == "5" or c == "6" or c == "7" or c == "8" or c == "9" or c == "." then
            validAmountString = true
        else
            validAmountString = false
        end
        print(validAmountString)
    end
    if string.gsub(addScreen.nameTxtField.text, "%s+", "") ~= "" and string.gsub(addScreen.amountTxtField.text, "%s+", "") ~= "" and validAmountString == true then
        --Proceed...
    end

The spaces part is working, but for some reason, when I try to check whether validAmountString == true in my if statement, it stops working. The validAmountString returns the correct boolean value according to my print statement though.

Any help? :)

Upvotes: 1

Views: 4626

Answers (3)

Jada Boba Fett
Jada Boba Fett

Reputation: 1

You do not need the loop to check whether it does or does not have valid characters.

You can simply do tonumber(string). It will return nil if there is any character other than a digit in the string.

Upvotes: 0

KingofGamesYami
KingofGamesYami

Reputation: 345

An easier way to go about this would be string.find

local validAmountString = not addScreen.amountTxtField.text:find( "[^%d%.]" )

I'm afraid I can't tell why validAmountString is not true, but you don't have to compare boolean values.

if (validAmountString) then
--is equivilent to
if (validAmountString == true) then

Upvotes: 1

Piglet
Piglet

Reputation: 28950

For checking your strings refer to KingOfGamesYami's answer. He's using something called string patterns btw. Check the Lua reference http://www.lua.org/manual/5.3/manual.html#6.4.1 for details.

"[^%d%.]" matches every character that not a digit or a dot. So if find returns something but nil, your string is invalid

For why your program isn't working as is (in case you want to stick to your complicated way:

You check every character of your string. If it is a number or a dot you asign true, if it's not you asign false.

So let's say you have "12A3" as a string. Then the final value of validAmountString is true because the last character is a number. So you assign true 2 times, then false for the "A" and after that you asign true again. You mess up your total result.

What you should do here is either assign validAmountString = validAmountString and true, else validAmountString = false so you can never overwrite a false, once set.

The even better and more efficient way would be to simply stop your check once you've hit an invalid character. There is no need to check further characters if you find one invalid. So simply add a break after validAmountString = false

Upvotes: 0

Related Questions