PDA

View Full Version : Convert input box entry to a number/integer



terrapods
October 8th, 2007, 12:09 PM
I have an input box which I enter a numerical value into, I want to compare this value with a variable (_total), but beause its a string it won't compare it to a number value, I've tried varius things including parseInt() but I can't seem to get it to work. Any ideas? Code so far below:

Thanks

var _total:Number = new Number()
var myAnswer:TextField = new TextField();
myAnswer.type = TextFieldType.INPUT;
myAnswer.maxChars = 2;
myAnswer.restrict = "0-9";


function onClick(event:MouseEvent):void {

myAnswer.text = Number(myAnswer1);

if (MyAnswer == (_total)) {
trace("Correct!");
} else {
trace("Wrong!");
}
}

Charleh
October 8th, 2007, 12:15 PM
I have an input box which I enter a numerical value into, I want to compare this value with a variable (_total), but beause its a string it won't compare it to a number value, I've tried varius things including parseInt() but I can't seem to get it to work. Any ideas? Code so far below:

Thanks

var _total:Number = new Number()
var myAnswer:TextField = new TextField();
myAnswer.type = TextFieldType.INPUT;
myAnswer.maxChars = 2;
myAnswer.restrict = "0-9";


function onClick(event:MouseEvent):void {

myAnswer.text = Number(myAnswer1);

if (MyAnswer == (_total)) {
trace("Correct!");
} else {
trace("Wrong!");
}
}

You can't do


myAnswer.text = Number(myAnswer1);

if (MyAnswer == (_total)) {

because myAnswer.text is a String field and therefore when you cast myAnswer1 to a Number it then get's cast back to a String on assignment to myAnswer.text.

Change it to



if(Number(myAnswer1) == _total) {
// Do somethin
}


On a side note where is myAnswer1 declared anyway?

terrapods
October 8th, 2007, 01:09 PM
Sorry I missed some code out (see below), which made it a bit confusing. Essentially the myAnswer is the name of the input box and I wanted to compare the number input into this text box to the variable _total.

I know this was a lot easier on AS2 but AS3 has made comparing strings/numbers impossible. I have a workaround under the onClick2 function, but its very convoluted (the correct possible answers are even numbers 0-20) so when one of these is entered in myAnswer, I have loads of IF statements to assign the values numerically to myAnswer1 so I can check it against _total. It works but I suspect there is an easier way of achieving the same result!

var _total:Number = new Number();
var myAnswer:TextField = new TextField();
var myAnswer1:Number = new Number();

myAnswer.type = TextFieldType.INPUT;
myAnswer.maxChars = 2;
myAnswer.restrict = "0-9";

function onClick2(event:MouseEvent):void {
if (myAnswer.text == "0") {
myAnswer1 = 0;
} else if (myAnswer.text == "2") {
myAnswer1 = 2;
} else if (myAnswer.text == "4") {
myAnswer1 = 4;
} else if (myAnswer.text == "6") {
myAnswer1 = 6;
} else if (myAnswer.text == "8") {
myAnswer1 = 8;
} else if (myAnswer.text == "10") {
myAnswer1 = 10;
} else if (myAnswer.text == "12") {
myAnswer1 = 12;
} else if (myAnswer.text == "14") {
myAnswer1 = 14;
} else if (myAnswer.text == "16") {
myAnswer1 = 16;
} else if (myAnswer.text == "18") {
myAnswer1 = 18;
} else if (myAnswer.text == "20") {
myAnswer1 = 20;
}
if (myAnswer1 == _total) {
trace("Correct!");
} else {
trace("Wrong!");
}
}

Charleh
October 8th, 2007, 01:15 PM
Did you try the fix I posted?



function onClick2(event:MouseEvent):void {
if (Number(myAnswer.text) == _total) {
trace("Correct");
} else {
trace("Wrong");
}
}


Don't know if Number converts in AS3 but whatever conversion function you can find should work - try myAnswer.text.toInt() maybe or int(myAnswer.text)

The reason your code wasn't working before was because you were assigning the conversion to int back to a string variable - so it was getting converted to int then back to string to get stored in myAnswer.text

terrapods
October 8th, 2007, 01:19 PM
That's great, Many thanks! :)