4 Replies Latest reply on Sep 18, 2006 10:30 PM by Newsgroup_User

    restricting chracters in editable text

    Level 7
      How can I restrict the characters in an edible text member? Say I want
      it to be limed to 10 characters that a user can enter.

      Thanks

        • 1. Re: restricting chracters in editable text
          Level 7
          Check the number of characters on keyDown and if it's at your limit then
          stop the key from being entered. Something like the following attached
          to your text sprite might get you started (add a GPDL handler to set the
          limit and make it more reuseable):
          --
          property spriteNum
          property myMember

          on beginSprite me
          myMember = sprite(spriteNum).member
          end

          on keyDown me
          tCharCount = myMember.text.char.count
          kc = _key.keyCode
          if tCharCount > 9 then
          -- pass left/right arrows, delete and backspace
          if [51, 123, 124, 117].getPos(kc) then
          pass
          else if kc = 115 then
          -- home
          myMember.selection = [0, 0]
          stopEvent
          else if kc = 119 then
          -- end
          myMember.selection = [tCharCount, tCharCount]
          stopEvent
          else
          beep
          stopEvent
          end if
          else
          if kc = 115 then
          -- home
          myMember.selection = [0, 0]
          stopEvent
          else if kc = 119 then
          -- end
          myMember.selection = [tCharCount, tCharCount]
          stopEvent
          else
          pass
          end if
          end if
          end
          • 2. Re: restricting chracters in editable text
            Level 7
            Thanks Sean.

            Just a few questions (showing my low knowlege of programming/Lingo)
            1. What's GPDL handler?
            2. You used: sprite(spriteNum).member
            but have seen it written as: sprite(me.spriteNum).member
            What's the difference?

            • 3. Re: restricting chracters in editable text
              Level 7
              > 1. What's GPDL handler?

              Much easier to type than getPropertyDescriptionList

              > 2. You used: sprite(spriteNum).member
              > but have seen it written as: sprite(me.spriteNum).member
              > What's the difference?

              You don't have to use me.spriteNum if you declare spriteNum as a
              property. Either is perfectly valid.
              • 4. Re: restricting chracters in editable text
                Level 7
                Chuck Neal has a behavior to limit characters in a text/field member at:
                http://www.mediamacros.com/item/item-945632531/
                and does a couple other things as well.

                His behavior has the getPropertyDescriptionList. The behavior lets you
                define which characters to recognise or to allow all. However, the code
                seemed to have a slight error. When I made it work for all characters,
                it did not limit the characters. Also, it did not allow Backspace,
                Delete or the arrow keys to be used when you reached the maximum
                character count. So, I edited the behavior and took a line from Sean's
                code to make it work with those keys not resricted. The only thing it
                doesn't do is allow you to write over characters with new ones. (But you
                can delete.)

                Here it is

                -- modified from Chuck Neal's mediamacros behavior
                property spriteNum, maxText, allowedKeys, myWrap

                on getPropertyDescriptionList me
                p_list = [:]
                p_list.addProp(#maxText, [#format : #integer, #comment : "Max number
                of allowed Characters:", #default : 20])
                p_list.addProp(#allowedKeys, [#format : #string, #comment : "Keys to
                pass to field (Leave blank for all):", #default :
                "1234567890abcdefghijklmnopqrstuvwxyz"])
                p_list.addProp(#myWrap, [#format : #boolean, #comment : "Allow word
                wrapping?", #default : 0])
                return p_list
                end

                on beginSprite me
                sprite(spriteNum).member.wordwrap = myWrap
                end

                on keyDown me
                if allowedKeys contains the key or allowedKeys = "" then
                if sprite(spriteNum).member.char.count > maxText then
                if [51, 123, 124, 117].getPos(the keycode) then pass
                dontpassevent
                else
                pass
                end if
                end if
                end

                on getBehaviorDescription me
                describe = "This is a simple behavior for text and field members that
                does the following..." & return
                describe = describe & "Limit a field to certain Characters" & return &
                "Limit a field to a certain NUMBER of characters" & "Turn the word wrap
                on/off for the field"
                end

                It all seems to be working for me with the above.

                regards
                Dean

                Director Lecturer / Consultant
                http://www.fbe.unsw.edu.au/learning/director
                http://www.multimediacreative.com.au