1 Reply Latest reply on May 25, 2009 11:08 PM by DawMatt

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

    sandwicense Level 1

      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
                              },

        • 1. Re: Edit Field Validation - Use of the native string.find() Lua Function
          DawMatt Level 3

          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