Skip to main content
Inspiring
May 25, 2009
Question

Edit Field Validation - Use of the native string.find() Lua Function

  • May 25, 2009
  • 1 reply
  • 1103 views

Hi,

I have an edit field in which I want to be validated immediately upon change.  I ONLY want to accept alphanumeric characters (a-z and 0-9).  I have tried this many ways, but have failed to get it exactly as I want it.  First I tried Sample Code A, but that line of code fails because combination strings that contain the alphanumeric characters and a special character like the pound sign (#), will validate (Example: "hello1#").  I tried Sample Code B, but that isn't 100% what I want because if you were to add a character that isn't available on the keyboard, such as the copyright symbol or the trademak symbol.  Can someone help me, please?  Thank you.

Sample Code A:

interface:edit_field {
                            value = bind { key = 'new_table', object = properties },
                            fill_horizontal = 1,
                            immediate = true,
                            validate = function (interface, value)

                              if string.find(value, "[a-z%d]")  then
                               
                                return true, value, "good"
                                   
                            else
                           
                                    return false, value, "failed"
                               
                                end
                            end
                        },

Sample Code B:

interface:edit_field {
                            value = bind { key = 'new_table', object = properties },
                            fill_horizontal = 1,
                            immediate = true,
                            validate = function (interface, value)
                               
                                if string.find(value, "[~!@#$%^&*()_+`-={}|\%[%]:\";'<>?,./]")  then
                                                        
                                return false, value, "failed"
                                   
                            else
                           
                                    return true, value, "good"
                               
                                end
                            end
                        },

This topic has been closed for replies.

1 reply

DawMatt
Inspiring
May 26, 2009

wallanceM wrote:

Hi,

I have an edit field in which I want to be validated immediately upon change.  I ONLY want to accept alphanumeric characters (a-z and 0-9).

...

Sample Code A:

interface:edit_field {
                            value = bind { key = 'new_table', object = properties },
                            fill_horizontal = 1,
                            immediate = true,
                            validate = function (interface, value)

                              if string.find(value, "[a-z%d]")  then
                               
                                return true, value, "good"

Hi,

Sample Code A seems the best approach to use.  Trying to detect unwanted characters would be error prone.

I noticed you are mixing character classes and ranges in your string.find pattern.  Have you tried using the following patterns instead?

                              if string.find(value, "[a-z0-9]")  then

or

                              if string.find(value, "[%l%d]")  then

Matt