0

Hello I have a problem with validating identical values in input boxes. Event called must be "on change" value of input box, but I have a problem with numbers.I want to write for example "17", but before there was value "1" already inputted in previous input box and it will report error because before we write 17 there is "1"- 7. So there is my question: is there a option to check identical values with this event type while dodging this error?

var textInput:Array = new Array();
for(var a:Number = 0;a < 2; a++){
    textInput[a] = new TextField();
    textInput[a].type = "input";
    textInput[a].y = 10+20*a;
    this.addChild(textInput[a]);
    textInput[a].addEventListener(Event.CHANGE, changeListener);
}

function changeListener (e:Event):void {
    if(Number(e.target.text)>22){
        e.target.text = "0";
    }
//problem area
    else{
        if(Number(textInput[1].text) == Number(textInput[0].text)){
            e.target.text = "0";
        }
    }
}

There is simple code with just 2 input boxes but in my project it more complex. How do define problem area to have possibility write "17" when we have "1" already in 1st input box.

Jasiu
  • 17
  • 7
  • It is very hard to follow your description, which is very vague. Please add the code that you have so far, how it is intended to behave and how it behaves instead. – null Jun 24 '15 at 11:41
  • Thank you, This helps. I cannot quite follow your explanation. Could you please write down what behaviour you want to create? You are describing what problems you have with your current solution, but you fail to explain (to me at least) what your original goal is. Given that you have a few text boxes, what exactly should the user experience, what is supposed to happen? Make a screenshot if you have a hard time explaining it. – null Jun 24 '15 at 12:07
  • 1) Text in inputbox1 = "1" 2) now user want to write: "11" in inputbox2, but he can't because if conditional change it value to "0" after he enter "1". how to write this conditional while allowing to enter "11". Maybe there is other method than using IF statment. – Jasiu Jun 24 '15 at 12:15
  • If you don't want to set the input box back to "0", why do you have code that does this? Again, don't explain what problem you have, explain what you want to do in the end. Why do you have these textboxes, why do you want to compare them, what's the point of all this? It's clear that your current solution doesn't work, you wouldn't ask here for help if it did. there's no need to elaborate n the current solution further, instead elaborate on the reason why you tried this solution and what functionality you want to achieve. – null Jun 24 '15 at 12:35
  • I have to check on change value of 22 input boxes with possible 22 (1..22) unique number values. After change I have to check if there is unique value in each box. If not then input box that triggered event have back to "0" value. – Jasiu Jun 24 '15 at 12:55
  • @JanekG. I think that using `FocusEvent.FOCUS_OUT` with `Event.CHANGE` to detect if the typed text is already exist, can help to get what you want. – akmozo Jun 24 '15 at 13:07
  • Ah I knew it, question was more about "if there is possibility", not about roundabout solution, but thanks :). – Jasiu Jun 24 '15 at 13:10
  • @JanekG. In your current case with the mentioned code, the answer is NO. But may be if you tell us why you need these 22 text boxes we can find another manner to solve your problem ? Using a combo box, or forcing user to type '02' for '2', or respecting some order ... – akmozo Jun 24 '15 at 13:15

1 Answers1

0

You can't both allow and disallow the same thing at the same time, obviously.

What you can do is validate the field on CHANGE and only mark it as valid or invalid (perhaps with some error styling) and on FOCUS_OUT you reset the text if it's not valid.

Something like this:

var valid:Boolean = true;
input.addEventListener(Event.CHANGE, validate);
input.addEventListener(FocusEvent.FOCUS_OUT, commit);

function validate(e:Event):void {
    var input:TextField = e.currentTarget as TextField;
    var value:Number = Number(input.text);
    if(value > 22){
        valid = true;
        input.backgroundColor = 0xff0000; // error style
    }else{
        valid = false;
        input.backgroundColor = 0xffffff; // normal style
    }
}

function commit(e:FocusEvent):void {
    if(!valid){
        var input:TextField = e.currentTarget as TextField;
        input.text = "0";
        input.backgroundColor = 0xffffff; // normal style
    }
}

Also I would recommend that you encapsulate this stuff in a class that extends TextField.

Aaron Beall
  • 49,769
  • 26
  • 85
  • 103