Can we have hexadecimal / Binary numbers in Text field and validate it?

Jan 29, 2018

Dear All,

I am developing a project for higher education where hexadecimal / Binary numbers are involved in final assessment. I am looking for an input field where User should enter a range of hexadecimal value (from 20000000 to 200FFFFF). And I should be able to validate its correctness.

Is this possible in Storyline2?

10 Replies
imgk hss

Hi Ashley, Thanks for your response. 

yes, using text entry field, user can insert both letters and numbers. My challenge is with validating the answer. We have almost 4000 possible correct answers (hexadecimal value from 20000000 to 200FFFFF) and i cant have them all 4000 entered manually (for validation). Is there a way to give this hexadecimal range for validation?

Thanks again for your help.

OWEN HOLT

It would look something like....
var testString = "your storyline variable";
function is_hexadecimal(testString) {
    regexp = /^[0-9a-fA-F]+$/;
        if (regexp.test(testString)) {
        return true;
        } else
       {
       return false;
       }
       }

var myTestHex = is_hexadecimal(testString);

That last variable will store either true or false and you can send that value back to SL.

OWEN HOLT

I realized my answer was only a partial answer in that my code only validated whether or not the number was a valid hexadecimal number.  Once you validate this, you would still need to check if it is within the range.  For me, the easy way to do this would be to convert your range values and the user input to integers and then evaluate the values.

Updated (and commented) code including the calls to StoryLine below (Note that I am using the following text variables in StoryLine: HexValue, IsValid, & IsBetween.

//Find the SL Player and store the user's input in a JS variable
var player = GetPlayer();
var testString = player.GetVar("HexValue");

//Create a function to test the user's input to determine if it is a valid Hex number
function is_hexadecimal(testString) {
regexp = /^[0-9a-fA-F]+$/;
if (regexp.test(testString)) {
return true;
} else {
return false;
}
}

//Run the function and send a true or false value back to SL
var myTestHex = is_hexadecimal(testString);
player.SetVar("IsValid", myTestHex)

//Convert valid hex to integer and evaluate if user input is within the specified value range
if (myTestHex == true) {
var x = parseInt("20000000", 16);
var y = parseInt(testString, 16);
var z = parseInt("200FFFFF", 16);
if (x<y && y<z) {
var validResult = "true";
player.SetVar ("IsBetween", validResult);
} else {
var validResult = "false";
player.SetVar ("IsBetween", validResult);
}
} else {
var validResult = "n/a";
player.SetVar ("IsBetween", validResult);
}

See it work (sl360) here.

SL2 file attached below.

Russell Killips

I was thinking of using a graded numeric quizzing slide and adding a text input field to the slide.

For the acceptable numeric values set it to:
Value is Between 536870912 and 537919487

Those are the decimal values of 20000000 and 200FFFFF

Then add a execute javascript trigger that converts the hexadecimal input value to a decimal value. Make sure the javascript trigger is before the submit interaction trigger.

var player = GetPlayer();

var q1Input = player.GetVar("q1Input");
var nDecimal = parseInt(q1Input, 16);

player.SetVar("q1Answer",nDecimal);

This discussion is closed. You can start a new discussion or contact Articulate Support.