Is there anything like IsNumeric?

I'm just wondering if there's anything like the function IsNumeric in Visual Basic?  I just want to check that what is entered in a text entry box is actually a number and not letters.

Comments

  • No, but you could do something like this:

    window.isNumeric = function (obj) {
    switch (typeof obj) {
    case "boolean":
    /* FALL-THROUGH */
    case "object":
    return false;
    case "string":
    obj = Number(obj);
    break;
    }
    return isFinite(obj) && !isNaN(obj);
    };
    The code will handle any type passed to it.  Returns true for actual numbers and numeric strings, that aren't also NaN or Infinity, and false for anything else.
  • Would I create a script, and then call that with something like
    <<if $buyhp.isNumeric = true>>
    or have I got it wrong?

    I've just tried it and when I put it in Twine (using SugarCube) using a <<textbox $buyhp "">> it's constantly referring false.  If I'm cpmpletely wrong I'm still trying to work out how Javascript works in Twine
  • You'd put that in a script-tagged passage, yes.  Usage would be:

    <<if isNumeric($buyhp)>>
    However, that function is already in SugarCube's Util library, so you could simply use that one if you wanted.  Usage would be:

    <<if Util.isNumeric($buyhp)>>

    Additionally:
    AntonMuerte wrote:

    using a <<textbox $buyhp "">>


    Unless you're doing something like symbolic reference (I doubt you are), then in SugarCube, per the docs, you must quote the name of the data $variable that you're passing into <<textbox>> (or &lt;&lt;checkbox&gt;&gt; or &lt;&lt;radiobutton&gt;&gt;).  For example:

    // OK
    <<textbox "$buyhp" "">>

    // WRONG
    <<textbox $buyhp "">>
    If you don't quote the $variable, then the macro processor will dereference it and pass its value to &lt;&lt;textbox&gt;&gt;, which isn't what you want.
  • Works perfect.  I suppose for simplicities sake I'll use the one built into SugarCube.  Sorry for not mentioning I was using SugarCube before  :-[

    I did have $buyhp in the quotes, that was just a typo because I didn't cut and paste.
Sign In or Register to comment.