-
Special Member
how to check if variable is numeric
in php we have a function called is_numeric(); ...this checks if the string is numeric or alphanumeric. what is ActionScript's alternative function similar to this?
Tea
-
I don't know precisely. Some suggestions are that if there is no chance the string could be simply a number, such as "6", then you can use
if (Number(a)) {
}
That execute the brackets only if a turns out to be numeric, or a string such as "6". "Hello" or "6hello" or "hello6" will not trigger it.
-
Just to be clear, you're trying to determine whether something is a number or not, right?
If so, you can use typeof(varName) to determine what it is.
-
Special Member
@ihateregisterin
Sorry mate, already tried that, but it's for checking if an element is a "STRING" or a "NUMBER".... I need to find out if a STRING is just numbers, or alphaNumeric.. it's like a field for the users to type in a COUNTER NUMBER, and Flash must check if they typed in NUMBERS only and no punctuation marks and letters. Thanks tho'
@rdoyle720
same as the above mate. Not what i needed exactly.
Any more suggestions?
-
Well in that case, you just want to disable the text field from accepting alpha and punctuation characters.
Look for the text.restrict under the actionscript dictionary.
-
Senior Member
Hi,
That should be possible to check using something like this,
code:
function is_numeric(val) {
if (!isNaN(Number(val))) {
return true;
}
return false;
}
var testArray = ["6", "hello", "1,2,4,8,16", "bleh1", "3.14", "743jhg"];
for (var i = 0; i < testArray.length; ++i) {
trace("testArray[" + i + "] = " + testArray[i] + ", is numeric? " + is_numeric(testArray[i]));
}
which outputs,
testArray[0] = 6, is numeric? true
testArray[1] = hello, is numeric? false
testArray[2] = 1,2,4,8,16, is numeric? false
testArray[3] = bleh1, is numeric? false
testArray[4] = 3.14, is numeric? true
testArray[5] = 743jhg, is numeric? false
If you wanted to not allow values with decimal places you could use the same idea to create an is_integer function,
code:
function is_integer(val) {
if (!isNaN(Number(val))) {
if (Math.floor(val) == Math.ceil(val)) {
return true;
}
}
return false;
}
-
Special Member
awsome catbert! Thanks! Appreciate it a lot!
Tea J
-
Originally Posted by catbert303
Hi,
That should be possible to check using something like this,
code:
function is_numeric(val) {
if (!isNaN(Number(val))) {
return true;
}
return false;
}
var testArray = ["6", "hello", "1,2,4,8,16", "bleh1", "3.14", "743jhg"];
for (var i = 0; i < testArray.length; ++i) {
trace("testArray[" + i + "] = " + testArray[i] + ", is numeric? " + is_numeric(testArray[i]));
}
which outputs,
testArray[0] = 6, is numeric? true
testArray[1] = hello, is numeric? false
testArray[2] = 1,2,4,8,16, is numeric? false
testArray[3] = bleh1, is numeric? false
testArray[4] = 3.14, is numeric? true
testArray[5] = 743jhg, is numeric? false
If you wanted to not allow values with decimal places you could use the same idea to create an is_integer function,
code:
function is_integer(val) {
if (!isNaN(Number(val))) {
if (Math.floor(val) == Math.ceil(val)) {
return true;
}
}
return false;
}
hi
i am exploring the environment of flash and this code is very important to me, could you tell me where should i put the code.? i mean in the first frame, onClipEvent, or etc.? i am new to flash thanks in advance.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|