Skip to main content
Participant
February 8, 2013
Answered

How to allow input text field to accept more than one "specific" answer.

  • February 8, 2013
  • 4 replies
  • 1515 views

Hi,

I am working for something and trying to create a type-box-based quiz for one of my classes, where an input text field can change it's border color if 2 or more words from an accepted word list is inputed.

For example, Say a list has possible answers: R,G, B, Y for the question "Name 2 colors".

I want to create an input text box where the border of the box will change color if any 2 letters (in the above list) are entered into it.

I've tried setting up a variable NumberSuccesses and setting it so that the number increases by 1 for each letter entered e.g.

if (Ex.text == "R");

NumSuc = NumSuc +1; // the var NumSuc has already been defined earlier in the code

If (Ex.text ''G")

NumSuc = NumSuc + 1

if (NumSuc == 2)

Ex.borderColor = 0x0000FF

but that didn't work.. and I tried doing it with another function which I use for multiple input boxes like if there were boxes A, B, C. I could set up a general function where if the correct answers were entered into A, B, an C respectively, only then will say, a checkmark show up using a

EnableCheckmark (); type function. Not sure how to do it if it's the same text input box though. Also just tried adding it into the same function re.

If (Ex.text == "R" + "G")

Ex.borderColor = 0x0000FF

.. Does anyone know what else I can do?

This topic has been closed for replies.
Correct answer moccamaximum

The solution of Nishu with Evaluation Logic:

//These Are the Possible Answers

var solutions:Array = new Array("A","B","C","D");

function testInput(_inputText:String):Boolean

{

    //the delimiter could also be a comma, here it is a space

    var inputStringArray:Array = _inputText.split(" ");

    var counter:Number = 0;

    for (var i:int=0; i<inputStringArray.length; i++)

    {

        for (var j:int=0; j<solutions.length; j++)

        {

            if (inputStringArray== solutions)

            {

                counter++;

            }

        }

    }

    if (counter >=2)

    {

        trace("true");

        return true;

    }

    else

    {

        trace("false");

        return false;

    }

}

//Textfield with name input_txt on stage

input_txt.addEventListener(TextEvent.TEXT_INPUT, answerTxtInp);

// The function that will be called by the event listener

function answerTxtInp(txtEvent:TextEvent):void

{

     // depending on the possible answers of characters, change the border  color

     if(testInput(input_txt.text)){

         input_txt.borderColor = 0xFF0000;

     }

     else{

         input_txt.borderColor = 0x000000;

     }

}

4 replies

Inspiring
February 10, 2013

Actually one can do it in a much more streamlined way. The code below also can handle case sensitivity unlike the code suggested by moccamaximum. You can also toggle case sensitivity depending on your needs:

// number of minimum answers

var minAnswers:int = 2;

var solutions:Array = ["blah", "bird", "caR", "Cow"];

// create regular exprression out of the array of solutions

var solutionsRE:RegExp = new RegExp("\\b(" + solutions.join("|") + ")\\b", "gi");

input_txt.addEventListener(TextEvent.TEXT_INPUT, answerTxtInp);

function answerTxtInp(e:TextEvent):void

{

          // depending on the possible answers of characters, change the border  color

          input_txt.borderColor = answersRE.test(input_txt.text) && input_txt.text.match(answersRE).length >= minAnswers ? 0xFF0000 : 0x000000;

}

Inspiring
February 10, 2013

Oh, and forgot to mention.

Approach I suggested allows for multi-word solutions.

If solutions array is ["blah", "bird", "car", "cow", "making things", "New York"]

If person enters, say, "New York" - code will detect that answer is correct. But if person enters just "New" or "York" - answer will not be treated as a correct one.

Inspiring
February 11, 2013

Now there is only one small issue:

How do we prevent that the user cheats by inputting for example "blah blah " ?

moccamaximumCorrect answer
Inspiring
February 8, 2013

The solution of Nishu with Evaluation Logic:

//These Are the Possible Answers

var solutions:Array = new Array("A","B","C","D");

function testInput(_inputText:String):Boolean

{

    //the delimiter could also be a comma, here it is a space

    var inputStringArray:Array = _inputText.split(" ");

    var counter:Number = 0;

    for (var i:int=0; i<inputStringArray.length; i++)

    {

        for (var j:int=0; j<solutions.length; j++)

        {

            if (inputStringArray== solutions)

            {

                counter++;

            }

        }

    }

    if (counter >=2)

    {

        trace("true");

        return true;

    }

    else

    {

        trace("false");

        return false;

    }

}

//Textfield with name input_txt on stage

input_txt.addEventListener(TextEvent.TEXT_INPUT, answerTxtInp);

// The function that will be called by the event listener

function answerTxtInp(txtEvent:TextEvent):void

{

     // depending on the possible answers of characters, change the border  color

     if(testInput(input_txt.text)){

         input_txt.borderColor = 0xFF0000;

     }

     else{

         input_txt.borderColor = 0x000000;

     }

}

kglad
Community Expert
Community Expert
February 8, 2013

what if they enter 3 "words" two of which are correct and one of which is not correct?  is their answer correct?

February 8, 2013

// Register an event listener that detects when something is written in the TextField

Ex.addEventListener(TextEvent.TEXT_INPUT, answerTxtInp);

// The function that will be called by the event listener

function answerTxtInp(txtEvent:TextEvent):void

{

     // depending on the possible answers of characters, change the border  color

     Ex.borderColor = 0x00FF00;    

}