Forum Discussion

JimTushinski's avatar
JimTushinski
Community Member
3 years ago

Struggling with some straightforward JavaScript for data validation in Storyline 360

I hope someone can point out what I'm doing wrong. I had a JavaScript developer come up with code that checks a text variable set from a text entry field to see if it contains exactly 8 alphabetic characters. If it does, great, a true value gets assigned to the Storyline variable and the user moves on to the next slide. If if contains anything else, then return a false value to the Storyline variable and show an error message. I have a true/false variable in Storyline called CCO_ID_RETURN_VALUE that is set to false by default. However I can't get SetVar to ever change the variable from false, no matter what gets entered in the text field.

Here is the JS code that is executed when the user enters their ID in a text field and hits a Continue button. The Execute JavaScript happens when the button is clicked.

var player = GetPlayer();
var CCO_Return;
var CCO;
CCO = player.GetVar('CCO_ID');
if (CCO.length > 8 || CCO.length < 8 || !regex.test(CCO)) CCO_Return = false;
else if (CCO.length === 8) CCO_Return = true;
player.SetVar('CCO_ID_RETURN_VALUE', CCO_Return);

I'm not great at JavaScript, so I'm completely stumped as to why CCO_ID_RETURN_VALUE never changes even when the text field has 8 alphabetic characters entered. I've heard that the single quotes can cause issues if you use an apostrophe instead, but on a Windows keyboard, the single quote and the apostrophe are the same and I've had other JavaScript run using single quotes. I'm grasping at straws!

Of course, I have a client breathing down my neck to get this working. The developer doesn't know anything about Storyline, so he came up with the validation code and I put in the GetVar and SetVar. I need to accept or reject what he's sent me, but until I can get this to work in Storyline, I can't accept or reject.

Fingers crossed that someone sees the obvious problem in what I'm trying to execute. And I sure wish Storyline would implement some data validation built in. That would be amazing!

Thanks.

Jim

  • Hi, Jim.

    Thank you for sharing the code!

    Since my knowledge in Javascript is very limited, I'm hoping our community members will chime in.

    I was able to get your code to work, checking if the variable has exactly eight characters with a few changes. See below:

    Windows 10 (1) 2022-02-02 at 1.59.03 PM

    Screen Recording 2022-02-02 at 02.05.45 PM

    I hope this helps!