A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: how to check if variable is numeric

  1. #1
    Special Member Tea_J's Avatar
    Join Date
    Dec 2000
    Posts
    991

    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

  2. #2
    Member
    Join Date
    Jan 2003
    Posts
    90
    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.

  3. #3
    Senior Member
    Join Date
    Apr 2002
    Posts
    2,849
    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.

  4. #4
    Special Member Tea_J's Avatar
    Join Date
    Dec 2000
    Posts
    991
    @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?

  5. #5
    Member
    Join Date
    Jan 2003
    Posts
    90
    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.

  6. #6
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    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;
    }


  7. #7
    Special Member Tea_J's Avatar
    Join Date
    Dec 2000
    Posts
    991
    awsome catbert! Thanks! Appreciate it a lot!

    Tea J

  8. #8
    Junior Member
    Join Date
    Nov 2009
    Posts
    1
    Quote Originally Posted by catbert303 View Post
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center