4 Replies Latest reply on May 29, 2007 4:00 PM by var_dating=porking

    Event Listener - Text Input

    var_dating=porking
      If I need to validate the data of a textInput field is this the proper syntax?

      private function validatePercent(alert):Void
      {
      if (percent.text >= (51)) {
      alert("Percent can't be greater than 50");
      }
      }

      percent.addEventListener("change", validatePercent);

      Thanks,
      -swo
        • 1. Re: Event Listener - Text Input
          Greg Dove Level 4
          percent.text is a string value so don't know how well it will fare in the comparison

          try using:

          if (Number(percent.text )>= (51))

          I presume when you say textInput field you mean a textInput component? If so then it looks ok.

          • 2. Re: Event Listener - Text Input
            var_dating=porking Level 1
            Hmmm... I'm not getting any syntax errors but I don't seem to be getting the alert when I input 60 into the field.... Could you take a look at my class file briefly?

            import mx.controls.TextInput;
            import mx.controls.RadioButton;
            import mx.controls.RadioButtonGroup;
            import mx.controls.Alert;

            class four01k extends MovieClip
            {
            var salary:TextInput;
            var percent:TextInput;
            var empContrib:TextInput;
            var compContrib:TextInput;
            var totalContrib:TextInput;
            var copercentContrib:TextInput;
            var radioGroup:RadioButtonGroup;
            var percentRadio1:RadioButton;
            var percentRadio24:RadioButton;

            public function onLoad():Void
            {
            salary.addEventListener("change", this);
            percent.addEventListener("change", this);
            empContrib.addEventListener("change", this);
            compContrib.addEventListener("change", this);
            totalContrib.addEventListener("change", this);
            percentRadio1.addEventListener("click", this);
            percentRadio24.addEventListener("click", this);
            }

            private function formatCurrency(value:Number):String
            {
            var cents:Number = Math.round(value*100);
            var result:String = Math.floor(cents/100) + ".";
            cents %= 100;
            if (cents < 10) {
            result += "0";
            }
            result += String(cents);
            return result;
            }

            private function roundThatNumber(theNumber:Number,thePlaces:Number) :Number
            {
            var theKey:Number=Math.pow(10,thePlaces);
            theNumber*=theKey;
            theNumber=Math.round(theNumber);
            theNumber/=theKey
            return theNumber;
            }

            private function calculate():Void
            {
            var mymaxcontrib:Number = Number(15500);
            var mycontrib:Number = Number(salary.text)*Number(percent.text)/(100);
            var mydifference:Number = Math.min(mycontrib, mymaxcontrib);
            empContrib.text = formatCurrency(mydifference);

            var themaxcontrib:Number = (6000);
            var theannualmatch:Number = Number(.50)* Number(empContrib.text);
            var uptopercent:Number = Number(.03)*Number(salary.text)/(1);
            var theannualdiff:Number = Math.min(theannualmatch, uptopercent);
            var thesmalldiff:Number = Math.min(theannualdiff, themaxcontrib);
            compContrib.text = formatCurrency(thesmalldiff);

            var econtrib:Number = Number(empContrib.text);
            var scontrib:Number = Number(compContrib.text);
            var totalcont:Number = Number(econtrib+scontrib);
            totalContrib.text = formatCurrency(totalcont);

            var mynum:Number = Number(percent.text);
            var thepercent = mynum;
            var blah = thepercent;
            if(mynum >= Number(6)) {
            blah = .03*100;
            } else if (mynum == Number(5)) {
            blah = .025*100;
            } else if (mynum == Number(4)) {
            blah = .02*100;
            } else if (mynum == Number(3)) {
            blah = .015*100;
            } else if (mynum == Number(2)) {
            blah = .01*100;
            } else if (mynum == Number(1)) {
            blah = .005*100;
            }
            copercentContrib.text = blah + "%";
            }

            private function calculatesemi():Void
            {
            var mymaxcontrib:Number = Number(15500);
            var mycontrib:Number = Number(salary.text)*Number(percent.text)/(100)/24;
            var mydifference:Number = Math.min(mycontrib, mymaxcontrib);
            empContrib.text = formatCurrency(mydifference);

            var themaxcontrib:Number = (6000);
            var theannualmatch:Number = Number(.50)* Number(empContrib.text);
            var uptopercent:Number = Number(.03)*Number(salary.text)/(24);
            var theannualdiff:Number = Math.min(theannualmatch, uptopercent);
            compContrib.text = formatCurrency(theannualdiff);

            var econtrib:Number = Number(empContrib.text);
            var scontrib:Number = Number(compContrib.text);
            var totalcont:Number = Number(econtrib+scontrib);
            totalContrib.text = formatCurrency(totalcont);

            var mynum:Number = Number(percent.text);
            var thepercent = mynum;
            var blah = thepercent;
            if(mynum >= Number(6)) {
            blah = 3;
            } else if (mynum == Number(5)) {
            blah = 2.5;
            } else if (mynum == Number(4)) {
            blah = 2;
            } else if (mynum == Number(3)) {
            blah = 1.5;
            } else if (mynum == Number(2)) {
            blah = 1;
            } else if (mynum == Number(1)) {
            blah = .05;
            }
            copercentContrib.text = blah + "%";
            }

            private function validatePercent(alert):Void
            {
            if (Number(percent.text) >= (51)) {
            alert("Percent can't be greater than 50");
            }
            }

            public function change(event:Object):Void
            {
            if (event.target == salary, percent); {
            calculateFunction(), validatePercent();
            }
            }

            public var calculateFunction:Function;
            public function click(event:Object):Void
            {
            switch (event.target) {
            case percentRadio1:
            calculateFunction=calculate;
            break;
            case percentRadio24:
            calculateFunction=calculatesemi;
            }
            calculateFunction();
            }
            }


            • 3. Re: Event Listener - Text Input
              Greg Dove Level 4
              I assumed alert was a function you had defined elsewhere... I can't see it.

              Do you want to show an Alert window/pop-up? This is probably not usual with the validation running off the "change" event in the TextInput. The change event occurs as you type, so this provides continuous feedback .

              You could have your own function alert which just places its argument (the alert message ) in a dynamic textfield somewhere alongside the TextInput. That way you could use it for other validation alerts as well.

              The other option is that you could use a NumericStepper component instead of TextInput if you want numbers between 0-50
              • 4. Re: Event Listener - Text Input
                var_dating=porking Level 1
                I like your idea of having a "floating alert" in a dynamic textfield. I demo'd a sample with the stepper and it wasn't very well liked.

                Could you show me a quicky sample using your idea?

                I don't know why but the start of things i don't do well, but i'm decent with taking a sample and tweaking it to my specs.

                Thanks in advance,
                Shane