Groups | Blog | Home
all groups > flash actionscript > may 2005 >

flash actionscript : How do you specify a decimal place in input text?


SDALW
5/2/2005 12:00:00 AM
I am trying to create an input box that is limited to numers. I'd like the user
to input the number and NO decimal. I'd like the output to read with a decimal.
Is there anyway to do this?
Here's an example
User input / output
initial/0.00
2/0.02
23/0.23
237/2.37
2379/23.79
23798/237.98
237986/2379.86
2379864/23798.64
The output should automatically display as the user inputs. Moving the digits
as needed.

I know that this is must be a number variable that is converted to a string.
But from there I have not had any luck.
I've seen a few postings, but none that specifically address input boxes. Any
suggestions?
Thanks a lot.
kglad
5/2/2005 12:00:00 AM
yourTF.restrict = "0-9";
yourTF.onChanged = function() {
this.text = Number(myS)/100;
};
kl = new Object();
myS = "";
kl.onKeyDown = function() {
if (48<=Key.getCode() && Key.getCode()<=57) {
myS += Key.getCode()-48;
}
};
Key.addListener(kl);
SDALW
5/2/2005 12:00:00 AM
I can't seem to get this to work.
I have my input text field on the screen - it's named data_txt with var data.
Do I need something else?

kglad
5/2/2005 12:00:00 AM
SDALW
5/3/2005 12:00:00 AM
Great - it's working, but I have two questions:
1. how can the user highlight and change a character? For example, if 24976 is
entered, it displays as 249.76. However, the user MEANT to enter: 24876 since
the correct output is 248.76. Right now if I try to highlight and delete it
isn't possible.
I tried sending them to another frame and using
data_txt = "0.00";
myS = "";
But when I input 1234, I get 11223344 the first time, 111222333444 the second,
1111222233334444 the third...etc. It seems to capture the key exponentially,
depending on how many times I've used the text entry.

2. If I enter 12340, the output reads 123.4 intead of 123.40.
Thanks so much for your help.
SDALW
5/3/2005 12:00:00 AM
I think I've figured out how to get rid of the double digits when I go to the
next frame, I just added the removeListener.
I still can't figure out how to allow changing of the text ( 24876 to 24856)
within the input box -
or the 0 showing (2490 displays as 24.9 intstead of 24.90).

Right now each frame with input text has this script:

stop();
Key.removeListener(kl);
var myS;
data = "0.00";
this.data_txt.restrict = "0-9";
this.data_txt.onChanged = function() {
this.text = Number(myS)/100;
};
kl = new Object();
myS = "";
kl.onKeyDown = function() {
if (48<=Key.getCode() && Key.getCode()<=57) {
myS += Key.getCode()-48;
}
};
Key.addListener(kl);

SDALW
5/3/2005 12:00:00 AM
I can't get it to capture the enter key when I use this script either. The
movie automatically reads as "wrong" without any key being pressed. I need to
be able to check the amount they input each time they press the enter key.
Suggestions?

stop();
var myS;
data = "0.00";
this.data_txt.restrict = "0-9";
this.data_txt.onChanged = function() {
this.text = Number(myS)/100;
};
kl = new Object();
myS = "";
kl.onKeyDown = function() {
if (48<=Key.getCode() && Key.getCode()<=57) {
myS += Key.getCode()-48;
}
};
Key.addListener(kl);

Key.removeListener(keyPressListener);
keyPressListener = new Object();
keyPressListener.onKeyDown = function() {
if (Key.isDown(13)) {
if (data() == 114.39) {
gotoAndStop("correct");
} else {
gotoAndStop("wrong");
}
}
};

kglad
5/4/2005 12:00:00 AM
data_txt.restrict = "0-9";
data_txt.onSetFocus = function() {
startRecord = 1;
};
data_txt.onChanged = function() {
if (myS.charAt(0) == "0") {
myS = "";
} else {
this.text = Number(myS)/100;
if (myS.charAt(myS.length-1) == "0" && myS.charAt(myS.length-2) == "0") {
this.text += ".00";
} else if (myS.charAt(myS.length-1) == "0") {
this.text += "0";
}
}
};
kl = new Object();
myS = "";
kl.onKeyDown = function() {
if (startRecord) {
if (Selection.getBeginIndex() != Selection.getEndIndex()) {
insertText = 1;
} else {
insertText = 0;
}
if (48<=Key.getCode() && Key.getCode()<=57) {
if (!insertText) {
myS += Key.getCode()-48;
} else {
myS = myS.slice(0,
Selection.getBeginIndex())+(Key.getCode()-48)+myS.slice(Selection.getEndIndex())
;
}
}
if (96<=Key.getCode() && Key.getCode()<=105) {
if (!insertText) {
myS += Key.getCode()-96;
} else {
myS = myS.slice(0,
Selection.getBeginIndex())+(Key.getCode()-96)+myS.slice(Selection.getEndIndex())
;
}
}
}
};
Key.addListener(kl);

SDALW
5/4/2005 12:00:00 AM
Okay - you are a genius. I apologize for this - seems that I cannot figure
anything out.
I've got stacks of Flash books and have looked at them all, but still am so
dense.
Anyway -
How would I check to see if myS is equal to what they were told to input?
That is the variable I am looking for right? The user would type 12345 and the
output would be 123.45I need to check and see if this is correct though.
I tried this with a button - but need it with Enter AND a button
Here's the code I put on the button and it sent all inputs to correct, not
just 12345- but everything.
Then I need to capture the keycode

on (release) {
trace (myS);
if (myS = 12345) {
gotoAndStop ("correct");
} else {
gotoAndStop ("incorrect");

}

}

Thanks so very much. I'm a teacher, not a programmer obviously.
kglad
5/4/2005 12:00:00 AM
in conditional (if) statements, flash uses a double equal sign:

on (release) {
trace (myS);
if (myS == "12345") { /* myS is a string, so you must have quotes */
gotoAndStop ("correct");
} else {
gotoAndStop ("incorrect");
}
}

and for your use of the enter key, you can amend the onKeyDown handler to:
kl.onKeyDown = function() {
if (Key.isDown(Key.ENTER)) { /* <--- NEW PART */
if (myS == "12345") {
gotoAndStop("correct");
} else {
gotoAndStop("incorrect");
}
}
if (startRecord) {
if (Selection.getBeginIndex() != Selection.getEndIndex()) {
insertText = 1;
} else {
insertText = 0;
}
if (48<=Key.getCode() && Key.getCode()<=57) {
if (!insertText) {
myS += Key.getCode()-48;
} else {
myS = myS.slice(0,
Selection.getBeginIndex())+(Key.getCode()-48)+myS.slice(Selection.getEndIndex())
;
}
}
if (96<=Key.getCode() && Key.getCode()<=105) {
if (!insertText) {
myS += Key.getCode()-96;
} else {
myS = myS.slice(0,
Selection.getBeginIndex())+(Key.getCode()-96)+myS.slice(Selection.getEndIndex())
;
}
}
}
};


SDALW
5/4/2005 12:00:00 AM
You are so smart. Do you teach classes in Flash?
I would love to take one.
Thanks again. I really appreciate it.
SDALW
5/4/2005 12:00:00 AM
I have something confusing happening with the script. I use it for teaching my
students about inputting into a calculator.
I give them the number - and they are to enter it into the input box. If they
get it wrong (enter key) then they get a Hint and are required to try again. I
will give them two hints before I show them how to do it.
If I do the lesson, then go back, the numbers come up exponentially. For
example if they are to put in 7500 - I use this script which you wrote (below).
The script is on each frame - pointing to hint7 or hint8 depending on where
they make the mistake. The first time works perfectly - but when I go back to
try it again,(the back or forward button loads the .swf) entering 7500
displays 7550000 and the next time through it displays as: enter 7500 - display
is 7555000000. The next time it was : enter 7500 got 7555500000000.

How do I stop that from happening?

//I added these
stop();
myS == "0.00";
data == "0.00";
Key.removeListener(kl);
//your script
data_txt.restrict = "0-9";
data_txt.onSetFocus = function() {
startRecord = 1;
};
data_txt.onChanged = function() {
if (myS.charAt(0) == "0") {
myS = "";
} else {
this.text = Number(myS)/100;
if (myS.charAt(myS.length-1) == "0" && myS.charAt(myS.length-2) == "0") {
this.text += ".00";
} else if (myS.charAt(myS.length-1) == "0") {
this.text += "0";
}
}
};
kl = new Object();
myS = "";
kl.onKeyDown = function() {
if (Key.isDown(Key.ENTER)) { /* <--- NEW PART */
if (myS == "7500") {
gotoAndStop("correct5");
} else {
gotoAndStop("hint7");
}
}
if (startRecord) {
if (Selection.getBeginIndex() != Selection.getEndIndex()) {
insertText = 1;
} else {
insertText = 0;
}
if (48<=Key.getCode() && Key.getCode()<=57) {
if (!insertText) {
myS += Key.getCode()-48;
} else {
myS = myS.slice(0,
Selection.getBeginIndex())+(Key.getCode()-48)+myS.slice(Selection.getEndIndex())
;
}
}
if (96<=Key.getCode() && Key.getCode()<=105) {
if (!insertText) {
myS += Key.getCode()-96;
} else {
myS = myS.slice(0,
Selection.getBeginIndex())+(Key.getCode()-96)+myS.slice(Selection.getEndIndex())
;
}
}
}
};
Key.addListener(kl);



kglad
5/4/2005 12:00:00 AM
you're welcome. and no, i don't teach flash. i'm a pediatrician. flash is
just one of my interests/hobbies.

i simplified the coding because it's becoming unwieldy and difficult to debug.
(the only changes that i think will stop the problem with returning to a
textfield that's already had entries is the onKeyDown handler.)

try the following:

stop();
data_txt.restrict = "0-9";
myS = "";
data_txt.onChanged = function() {
if (this.text.charAt(0) == "0" && this.text.length == 1) {
this.text = "";
// eliminates initial zeros input by user.
} else {
recordString(data_txt);
displayTF(data_txt);
}
};
function recordString(tf) {
if (Key.getCode() == 8) {
myS = myS.slice(0,
Selection.getBeginIndex())+myS.slice(Selection.getEndIndex()+1);
} else if (48<=Key.getCode() && Key.getCode()<=57) {
myS = myS.slice(0,
Selection.getBeginIndex()-1)+(Key.getCode()-48)+myS.slice(Selection.getEndIndex(
)-1);
} else if (96<=Key.getCode() && Key.getCode()<=105) {
myS = myS.slice(0,
Selection.getBeginIndex()-1)+(Key.getCode()-96)+myS.slice(Selection.getEndIndex(
)-1);
}
if (Selection.getEndIndex()>=tf.text.length-1) {
setEnd = 1;
} else {
setEnd = 0;
}
}
function displayTF(tf) {
tf.text = Number(myS)/100;
if (myS.charAt(myS.length-1) == "0" && myS.charAt(myS.length-2) == "0") {
tf.text += ".00";
} else if (myS.charAt(myS.length-1) == "0") {
tf.text += "0";
}
if (setEnd) {
Selection.setSelection(tf.text.length, tf.text.length);
}
}

kl = new Object();
kl.onKeyDown = function() {
if (Key.isDown(Key.ENTER)) {
/* <--- NEW PART */
if (myS == "7500") {
_root.gotoAndStop("correct5");
myS = "";
} else {
_root.gotoAndStop("hint7");
myS = "";
}
}
};
Key.addListener(kl);
kglad
5/4/2005 12:00:00 AM
MichaelBlum
7/1/2005 1:50:39 PM
kglad, you are amazing. I worked on this all day yesterday and got it to work
sort of but not completely here's what I managed:

stop();
data_txt.border = 1;
data_txt.restrict = "0-9";
myS = "";
data_txt.onChanged = function() {
recordString(data_txt);
displayTF(data_txt);
};
function recordString(tf) {
tIndex = 0;
nIndex = 0;

switch (Selection.getCaretIndex()) {
case 0 :
tIndex = 0;
nIndex = 2;
break;
case 1 :
tIndex = 0;
nIndex = 2;
break;
case 2 :
tIndex = 0;
nIndex = 2;
break;
case 3 :
tIndex = 1;
nIndex = 3;
break;
case 4 :
tIndex = 2;
nIndex = 4;
break;
case 5 :
tIndex = 3;
nIndex = 7;
break;
case 6 :
tIndex = 3;
nIndex = 7;
break;
case 7 :
tIndex = 3;
nIndex = 7;
break;
case 8 :
tIndex = 4;
nIndex = 8;
break;
case 9 :
tIndex = 5;
nIndex = 9;
break;
case 10 :
tIndex = 6;
nIndex = 11;
break;
case 11 :
tIndex = 6;
nIndex = 11;
break;
case 12 :
tIndex = 7;
nIndex = 12;
break;
case 13 :
tIndex = 8;
nIndex = 13;
break;
case 14 :
tIndex = 9;
nIndex = 14;
break;
default :
tIndex = -1;
}

if (tIndex != -1) {
if (Key.getCode() == 8) {//backspace
myS = myS.slice(0, tIndex) + myS.slice(tIndex+1);
}
else if (48<=Key.getCode() && Key.getCode()<=57) {//0-9
myS = myS.slice(0, tIndex-1) + (Key.getCode()-48) + myS.slice(tIndex-1);
}
else if (96<=Key.getCode() && Key.getCode()<=105) {//0-9
myS = myS.slice(0, tIndex-1) + (Key.getCode()-96) + myS.slice(tIndex-1);
}
}

//trace(myS);
}

function displayTF(tf) {

tf.text = "(";

if (myS.charAt(0) != "") tf.text += myS.charAt(0);
else tf.text += "_";

if (myS.charAt(1) != "") tf.text += myS.charAt(1);
else tf.text += "_";

if (myS.charAt(2) != "") tf.text += myS.charAt(2);
else tf.text += "_";

tf.text += ") ";

if (myS.charAt(3) != "") tf.text += myS.charAt(3);
else tf.text += "_";

if (myS.charAt(4) != "") tf.text += myS.charAt(4);
else tf.text += "_";

if (myS.charAt(5) != "") tf.text += myS.charAt(5);
else tf.text += "_";

tf.text += "-";

if (myS.charAt(6) != "") tf.text += myS.charAt(6);
else tf.text += "_";

if (myS.charAt(7) != "") tf.text += myS.charAt(7);
else tf.text += "_";

if (myS.charAt(8) != "") tf.text += myS.charAt(8);
else tf.text += "_";

if (myS.charAt(9) != "") tf.text += myS.charAt(9);
else tf.text += "_";


Selection.setSelection(nIndex,nIndex);
trace(nIndex);

}

kl = new Object();
kl.onKeyDown = function() {
if (Key.isDown(Key.ENTER)) {
/* <--- NEW PART */
if (myS == "7500") {
_root.gotoAndStop("correct5");
myS = "";
} else {
_root.gotoAndStop("hint7");
myS = "";
}
}
};
Key.addListener(kl);
AddThis Social Bookmark Button