4 Replies Latest reply on Jul 12, 2009 1:16 AM by Dean Utian

    Text Field filter out anything other than "0123456789" and put it back into the field

    MAC800 Level 1

      First of all

      using D11

      Windows XP

       

       

      Question

       

      Lets say text member "Data" = "[434]"

       

      And I only want the text of member "Data" to have digits and no other characters. What script can you use to remove  them?

       

      So "[434]" would  become  "434"

       

       



      I had something like the below ... got a headache and gave up....   anyone help?

       

      on enterFrame

      legalcharacters = "0123456789"

      member("Data").text = legalcharacters(member("Data")

      end

       

      Am I way off?

        • 1. Re: Text Field filter out anything other than "0123456789" and put it back into the field
          Dean Utian Adobe Community Professional

          Hi Mac,

           

          You can get the value of  a string with value().

           

          Say you have a string  "[434]". This may be member("Data").text

           

          Data =  member("Data").text
          put Data
          -- "[434]"

          put value(Data)
          -- [434]

           

          put (value(Data))[1]
          -- 434

           

          Data = (value(Data))[1]
          member("Data").text  = string(Data)
          put  member("Data").text
          -- "434"

           

          Getting the value of a  string "[434]" returns a list. That's why you need the extra step.

           

          If the text was something  like "a123" then the value of this string would be VOID as  "a" cannot be converted to a value. However, you can use 'the  itemDelimiter'. This lets you set a special character (like "a") to  be used to separate items.

           

          the itemDelimiter =  "a"
          Data =   "a123"

          put Data.item[2]
          -- "123"

           

          put value(Data.item[2])
          -- 123

           

          Dean

           

          Director Lecturer /  Consultant / Director Enthusiast
          http://www.deansdirectortutorials.com/
          http://www.multimediacreative.com.au

          • 2. Re: Text Field filter out anything other than "0123456789" and put it back into the field
            MAC800 Level 1

            Re:

             

            Data = (value(Data))[1]

            member("Data").text = string(Data)

            put member("Data").text

             

            I get most of the above.  But why is the [1] needed in Data = (value(Data))[1] ?

             

            Conclusion ----  So if I understand correctly

            Then all I need is the following?

             

            on enterFrame

               Data = (value(Data))[1]

               member("Data").text = string(Data)

            end


                  

            • 3. Re: Text Field filter out anything other than "0123456789" and put it back into the field
              Sean_Wilson Adobe Community Professional
              Lets say text member "Data" = "[434]"

               

              And I only want the text of member "Data" to have digits and no other characters. What script can you use to remove  them?

               

              So "[434]" would  become  "434"

              Dean gave you a possibility, but his solution will only work to remove [ and ] characters, and then only if they are the first and last chars in the string of text.

               

              Can you be more specific about where your data is coming from? If it is user entered, then do you also need to filter alphabet characters out? You could use a JavaScript member and its regular expression object for a more general approach:

              -- in my message window:
              s = "1234abcg465(&*^"
              put s
              -- "1234abcg465(&*^"
              put jsReplace(s, "\D", "")
              -- "1234465"
              

              Here's the jsReplace() function - it should go into a #movie script whose scriptSyntax is set to #JavaScript:

              function jsReplace(inStr, strFind, strReplace){
                // error check input
                if(typeof(inStr)      != "string") { return inStr; }
                if(typeof(strFind)    != "string") { return inStr; }
                if(typeof(strReplace) != "string") { return inStr; }
                // search-and-replace
                re = new RegExp(strFind, "g");
                return inStr.replace(re, strReplace);
              }
              
              • 4. Re: Text Field filter out anything other than "0123456789" and put it back into the field
                Dean Utian Adobe Community Professional

                >I get most of the above.  But why is the [1] needed in Data = (value(Data))[1] ?


                If we look at the text string - "[434]" - we see square brackets at the start and end. So, if we convert the string to a value, we get - [434] - which is a list. The [1] retrieves the first item in the list (which is the only item in the list). For more info on lists, look at:

                http://www.deansdirectortutorials.com/Lingo/lists.htm


                dataString = "[434]"

                dataList = value(dataString)

                dataInteger = dataList[1]


                So, the above grabs the value 424 by converting the text into  a list, then grabbing item 1 in this list.


                My second suggestion was to use - the itemDelimiter =  "a"


                dataString "a434"

                the itemDelimiter =  "a"

                dataPartialString = dataString.item[2]


                The above assumes you have a character like 'a' that acts as the separater for numerical characters. Item 1 would be before the 'a' which wouuld be nothing (""). Item 2 is the characters after the first 'a' (up until the next 'a' if one exists).


                The above would only work if you know what character is going to be at the start the numerical sequence. If you need something more flexible, then Sean's jsReplace approach would be the best way to go.


                Dean


                 

                Director Lecturer /  Consultant / Director Enthusiast
                http://www.deansdirectortutorials.com/
                http://www.multimediacreative.com.au