6 Replies Latest reply on Feb 11, 2008 2:13 PM by dimival

    Currency Validation


      I am trying to validate the currency in a text input. The currency could be negative, must be not more than two decimal place. This is the code that I have.

      <mx:CurrencyValidator id="valueValidator" currencySymbol="$" precision="2"
      allowNegative="true" source="{value}" property="text" />

      <mx:TextInput id="value" paddingLeft="0" paddingRight="0"
      change="{testVO.value = Number(value.text)}" text="{testVO.value}"/>

      The problem that I am having is when the user types something that it cannot accept, it put's a value 'NaN', and I can't change this value or delete it. So this is like crashing the screen and the user can't change anything. How can I fix this? By the way the 'testVO.value' is of type 'Number'. Thanks.
        • 1. Re: Currency Validation
          Gregory Lafrance Level 6
          You could create testVO using the constructor, so you know it is initialized with a value:
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()">
          private var testVO:Number = new Number();
          <mx:CurrencyValidator id="valueValidator" currencySymbol="$" precision="2"
          allowNegative="true" source="{txtvalue}" property="text" />

          <mx:TextInput id="txtvalue" paddingLeft="0" paddingRight="0"
          change="{testVO = Number(txtvalue.text)}" text="{testVO}"/>
          • 2. Re: Currency Validation
            dimival Level 1
            I think it is because you are casting whatever the user types into a Number, so for example if the user types "Hello" your code tries to cast into a Number, which obviously results in NaN (Not a Number).

            There are many ways to avoid this, for example:

            - Use the isNaN() function before casting, if isNaN returns true then you can't cast the text into a Number:
            change="testVO.value = isNaN(value.text)?0:Number(value.text)"

            Additionally you could restrict the characters the user can type in the TextInput using the restrict property.:
            <mx:TextInput id="value" restrict="0-9.\-" ..................>
            This way the user can only type numbers and the . and - symbols.

            That's just an example of how to solve it, you can try other solutions just keep in mind the problem in your code is that you try to cast whatever the user types into a Number.

            Hope this helps you out
            • 3. Re: Currency Validation
              NewbieToFlex Level 1
              Hello Dimival,

              Thanks for your response. Your suggestion fixed the problem with the NaN. But, the problem that I have now is. If the user types '123.00.', it put's reset the textinput to '0'. How can I block the user from entering more than one decimal? Thanks.
              • 4. Re: Currency Validation
                dimival Level 1
                I think you need to do some code validation before casting the input into a Number and before assigning the value to the TextInput's text property.

                I suggest you write a method for checking the number of decimals in the TextInput, then if it is valid (according to your needs) cast it and then assign it to the TextInput.

                You can use a regular expression to perform the validation process, there's a whole chapter about regular expressions in the Programming ActionScript 3.0 document ( http://www.adobe.com/support/documentation/en/flex/)

                • 5. Re: Currency Validation
                  NewbieToFlex Level 1
                  Hi Dimival,

                  Thanks for the help. I have figured it out a way to get around this. I call a function on 'focusOut' event for the TextInput and in that I do the following:

                  var valid:ValidationResultEvent = valueValidator.validate();
                  if(valid.result == null)
                  // save to testVO
                  • 6. Re: Currency Validation
                    dimival Level 1
                    Great, you found a way around :D
                    Don't forget to mark this topic as closed