8 Replies Latest reply on Sep 27, 2011 10:53 AM by logeye

    Changing the cursor position in an input text field and Removing leading numeric zeros

    logeye Level 1

      Flash8 - AS2

       

      It's good to be back after all these years and thanks for the faithfull folks like kglad, Ned Murphy, Rothrock and all the others who give so much of themselves to make this forum a success!

       

      My Problem:

       

      I have an input text field that is named in the properties box as 'numberOfKids'.

      When the program is executed, and a delete or backspace is pressed the input textfield goes blank

      and any calculations that use 'numberOfKids' shows NaN.

       

      I have trapped the NaN and undefined so that a numeric 0 shows up instead of a blank.

       

      There are two problems:

       

      First problem:

        The cursor is to the left of the zero, so the first number inputted by the user is 10x larger than what they want.  0 becomes 10.

      Question:  How do I get the cursor to move to the right of the 0?

       

      Second Problem: 

        When I manually move the cursor to the right of the 0 and input a number, say 4, it shows up as 04.

      Question 2:  How do I delete the leading numeric zero in the input textfield?

       

      Thanks for the help.

       

       

      Here's the code I am using:

       

      calculateChildWeight = function () {
      if (isNaN(numberOfKids)) {
        numberOfKids = 0;
      }
      if ((numberOfKids) == undefined) {
        numberOfKids = 0;
      }
      if (numberOfKids>=0 && numberOfKids<=137) {
        numberOfKids = numberOfKids;
      } else {
        numberOfKids = 0;
      }
      if (isNaN(adjustedChildWeight)) {
        adjustedChildWeight = 0;
      }
      if ((adjustedChildWeight)=undefined) {
        adjustedChildWeight = 0;
      }
      adjustedChildWeight = Number(numberOfKids*(-100));
      //numberOfKids.setSelection(numberOfKids.length, numberOfKids.length);
      //if (numberOfKids.length=2 && numberOfKids<10) {
        // remove the first character or leading zero
      //  numberOfKids = numberOfKids;
      // }
      };

        • 1. Re: Changing the cursor position in an input text field and Removing leading numeric zeros
          Rothrock Level 5

          There are a number of ways to deal with this.

           

          You can use the various events of the textfield class to recognize when the textfield has been entered, changed, or left and then do things with the text.

           

          Generally I'm guessing that the number of children is never negative or a letter, so using the restrict property of the textfield to only allow the digits 0 through 9.

           

          And while it is highly unlikely it is possible that the number of kids might be two digits. Pretty sure that there will be no three digit number of kids!

           

          So you could use the change event to limit the length to two places.

           

          There are a lot of other things that could be done, but it is difficult to know without knowing all the details of what you are trying to do.

          • 2. Re: Changing the cursor position in an input text field and Removing leading numeric zeros
            logeye Level 1

            Thanks for the reply Rothrock,

             

            I am making an electronic weight and balance document for aviation training.  The number of children can be up to the number of seats available on the aircraft.

            (ie 3 digits.)  From that number the total weight of the children is calculated.  The calculated number is used in the weight and balance calculations.

             

            The problem really exists in all of my text input fields with Flash 8 as2.

             

            After each key stroke I run a function that recalculates everything, stores data in shared objects etc.

             

            If  the backspace or delete key clears the text input field, I get NAN.  I test for this and assign a zero to the field.

             

            The cursor if placed manually typically goes to the left of the zero (it's right justified) then any inputted numbers are multiples of 10.

             

            I need to reposition the cursor to the right of the zero and delete the zero after a number is inputted.  This is my main goal.

             

            Thanks again for your contribution to this forum.

            • 3. Re: Changing the cursor position in an input text field and Removing leading numeric zeros
              Rothrock Level 5

              I would probably not replace the value in the textfield with a visible zero, just in the calculation.

               

              Next I would probably make the zero be selected so the next entry by the user would just replace the zero.

               

              After that I would use the onChange event to see if the start of the string was a zero and strip it off.

               

               

               

              The first is my favorite because it doesn't have the possibility for the user to accidently enter a 20 when they meant to put in a 2.

               

              The second avoids this problem if they just type, but given that some people might click and type it could still have problems and would require other measures to prevent 0X and X0 (where only X was intended).

               

              The last is okay, but it smacks of magic and I think lots of folks wouldn't like to have 09 automagically change to 9.

               

              So which approach do you want to take?

              • 4. Re: Changing the cursor position in an input text field and Removing leading numeric zeros
                logeye Level 1

                Thanks Rothrock.

                 

                It seems like leaving the textfield blank but still passing the value of 'zero' to other caluclations would be the best solution.

                 

                In your comments I get the feeling I'm not using the best practices for this.

                 

                What I did was to create an input text field and in the properties box assigned a variable name to it.

                I 'typed' the variable as a number (var numberOfKids:Number=0;) in the action script then performed calculations on the numeric variable.

                 

                I'm kinda wishing I hadn't gone away from Flash for those three years or so.

                 

                Thanks for your patience and suggestions.

                • 5. Re: Changing the cursor position in an input text field and Removing leading numeric zeros
                  Rothrock Level 5

                  Yeah you shouldn't be using the var property of a textfield.

                   

                  Instead use the text property of the textfield.

                   

                  You can then use parseInt(myTextField.text) to change the text value into a number. (I'm assuming you don't have any fractional kids! But if you did then you might want to use parseFloat().)

                   

                  It looks like you already have a handle on how to use the onChange and entering and leaving a text field. So I'm thinking you'll be able to get this from here. Let me know if you get hung up.

                  • 6. Re: Changing the cursor position in an input text field and Removing leading numeric zeros
                    logeye Level 1

                    Thanks Rothrock for being so  patient.

                     

                    I'm lost!

                     

                    I go back to a brand new flash file to try and implement the textfields as a stand alone test document.  I have lost the concept I think.

                    I created on the stage of a blank .fla file  4 text fields, 2 input and 2 dynamic.  I gave them an instance name in the properties box.  I use this instance name.text to find the value of the field.  I use the parseInt to put that value into a numeric variable for calculation.  The results are put to the appropriate textfield.

                     

                    All seems to work except one of my input text fields.   I use the value of this field  to calculate:

                     

                    revisedValue = parseInt(numberOfKids.text);
                    revisedValue = revisedValue*(-100);

                     

                    The problem now is that if the numberic '2' is inputted into numberOfKids, numberOfKids.text shows up as 200 and a trace shows up as '2NaN'.

                     

                    Flash is logical and consistant, I must conform to it but I'm in the dark.

                     

                    (( Concerning the forum:  If I click on helpful, which your posts have been in directing me, does that blank out the possibility of selecting the correct answer in the future???))

                    • 7. Re: Changing the cursor position in an input text field and Removing leading numeric zeros
                      Rothrock Level 5

                      Most important answer first: You get two helpfuls and one correct answer per thread. I think once you give a specific response a helpful you can't give the same one an answer. But I don't know.

                       

                       

                      First make sure that none of those text fields have a vars property set.

                       

                      Also you don't need to do it in two steps: revisedValue = -100*parseInt(numberOfKids.text)

                       

                      Just like you read the value of your input textfields using the text property, you will also set the value of the dynamic ones using the text property. That is what you are doing, right?

                      • 8. Re: Changing the cursor position in an input text field and Removing leading numeric zeros
                        logeye Level 1

                        Thanks again for your patience.

                         

                        I'm having to abandon the project due to no time available for a few months  Your answers have been helpful and I want to restart the project from scratch using your suggestions.  The first project was built on the fly and worked OK but it was not polished.  Adding to it now is the problem.

                         

                        I'll try and go back and give you a correct answer for your help.

                         

                        Thank you again for your patience.