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.
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 <<checkbox>> or <<radiobutton>>). 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 <<textbox>>, which isn't what you want.
Comments
true
for actual numbers and numeric strings, that aren't alsoNaN
orInfinity
, andfalse
for anything else.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
script
-tagged passage, yes. Usage would be: However, that function is already in SugarCube'sUtil
library, so you could simply use that one if you wanted. Usage would be:Additionally:
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
<<checkbox>>
or<<radiobutton>>
). For example: If you don't quote the $variable, then the macro processor will dereference it and pass its value to<<textbox>>
, which isn't what you want.I did have $buyhp in the quotes, that was just a typo because I didn't cut and paste.