Help: How to handle special characters in TextField()

Hello Guys, 

I’ve been looking the the solution of my problem, but none seems to works.

My problem is when the user Input thru key device keyboard, he/she can put the any special characters she likes but I want to take that freedom because I’ll be using a database to save that user put on TextField, my prop is that what is the user puts " (double quote on the textField) I know will it produce and error,

I want to make a handler which examine the string by user and when it finds any special characters it will prompt the user that he/she puts an invalid character on the textField.

Please help me guys,

And Thank you in advance

Jam

Hi. This is a common problem. You could try to address the issue at the sql level and “escape” the problem characters so the user can enter them and get away with it. Is that a preferable option compared to not allowing these characters? 

Hi Ksan, thanks for the reply.

That may have solve my problem but, I just want to restrict the user from entering special characters, because I’m gonna ask for the users name, which could only consist of letters, lower and upper cases. 

Here is most the code I use where I do not allow special characters and few other things. 

[lua]

local createUserEvent = function (event )

    

    if event.phase == “release” then

        

        – I’m attaching username input box to scene

        local userName = scene.userNameInput.text or ‘’

        

        if string.len(userName) < 2 then

            – Show alert “Too short”

            return

        elseif string.len(userName) > 20 then

            – Show alert “Too long”

            return

        elseif userName:match("%W") then 

            – %W represents all alphanumerical characters

            – Show alert “Only use a-z, 0-9” 

            return

        end

                        

        – If we reach here we are good 

        – Do whatever 

    end

end

[/lua]

thanks for replying jonjonsson,

it worked!! but everytime I input a name that sa _ (space in it), it returns that the text in invalid.

does this detect the space and says it was invalid?

thanks,

Jam 

Hi. This is a common problem. You could try to address the issue at the sql level and “escape” the problem characters so the user can enter them and get away with it. Is that a preferable option compared to not allowing these characters? 

Hi Ksan, thanks for the reply.

That may have solve my problem but, I just want to restrict the user from entering special characters, because I’m gonna ask for the users name, which could only consist of letters, lower and upper cases. 

Here is most the code I use where I do not allow special characters and few other things. 

[lua]

local createUserEvent = function (event )

    

    if event.phase == “release” then

        

        – I’m attaching username input box to scene

        local userName = scene.userNameInput.text or ‘’

        

        if string.len(userName) < 2 then

            – Show alert “Too short”

            return

        elseif string.len(userName) > 20 then

            – Show alert “Too long”

            return

        elseif userName:match("%W") then 

            – %W represents all alphanumerical characters

            – Show alert “Only use a-z, 0-9” 

            return

        end

                        

        – If we reach here we are good 

        – Do whatever 

    end

end

[/lua]

thanks for replying jonjonsson,

it worked!! but everytime I input a name that sa _ (space in it), it returns that the text in invalid.

does this detect the space and says it was invalid?

thanks,

Jam 

Up for this… jon’s code works, but it doesn’t detect space

Change the match expression to 

[lua]

elseif userName:match(’[^a-zA-Z0-9]’) then 

[/lua]

That does the trick :slight_smile: thanks Jon

Up for this… jon’s code works, but it doesn’t detect space

Change the match expression to 

[lua]

elseif userName:match(’[^a-zA-Z0-9]’) then 

[/lua]

That does the trick :slight_smile: thanks Jon