Forum Discussion
Text Entry Minimum Character Requirement
- 2 months ago
Hi NickiMazzocca-8,
I have provided a storyline file with an example of this. It does require JavaScript, but you should just be able to add to your project.
Here's the outcome on Review 360 before you get into it.
In my example, I have added a message below the text area indicating how many words have been typed.
This solution uses two variables, these are:
name: minWords
Type: Number
Value: 50name: wordCountMessage
Type: Text
Value:name:wordCountReached
Type: True/False
Value: False
I then add an input box to the slide, and ensure it is set to Multi-line, but selecting the input box, and selecting CTRL+ENTER and then selecting Text Box and then Text Entry > Multi-line.I then add a single line Text Box below the Multi-line text input, and add the variable %wordCountMessage% to the contents of the Text Box. The variable name is displayed with a yellow background, to indicate it is a variable reference, and dynamic text.
I then add an icon to the slide which has two states, to visually indicate if the minimum requirement has been met. The Normal state is a cross icon and the Tick state is a tick icon. I use the variable wordCountReached to switch between the two states.
I then add a trigger to change the state of the icon when the variable wordCountReached is changed between True/False. True = Tick (Tick state of icon), False = Cross (Normal state of icon)
I then add the following JavaScript to the "timeline starts on this slide - Execute JavaScript" trigger. This will control the variables in Storyline which will either update text, or cause the icon to switch to the Tick or Cross.
// Get the Storyline Player const player = GetPlayer(); // Get the text area const textarea = document.querySelector('textarea'); // Get the number of minimum words required const minWords = player.GetVar('minWords') // Add the event listener textInput = function() { const text = textarea.value.trim(); // Get the text and trim whitespace const words = text.split(/\s+/).filter(word => word.length > 0); // Split text into words const wordCount = words.length; // Count the words player.SetVar('wordCountMessage',`You have typed ${wordCount} words. Minimum required is ${minWords}.`); player.SetVar('wordCountReached',wordCount >= minWords); } textarea.addEventListener('input', textInput); // run to init textInput();
Hi NickiMazzocca-8,
I have provided a storyline file with an example of this. It does require JavaScript, but you should just be able to add to your project.
Here's the outcome on Review 360 before you get into it.
In my example, I have added a message below the text area indicating how many words have been typed.
This solution uses two variables, these are:
name: minWords
Type: Number
Value: 50
name: wordCountMessage
Type: Text
Value:
name:wordCountReached
Type: True/False
Value: False
I then add an input box to the slide, and ensure it is set to Multi-line, but selecting the input box, and selecting CTRL+ENTER and then selecting Text Box and then Text Entry > Multi-line.
I then add a single line Text Box below the Multi-line text input, and add the variable %wordCountMessage% to the contents of the Text Box. The variable name is displayed with a yellow background, to indicate it is a variable reference, and dynamic text.
I then add an icon to the slide which has two states, to visually indicate if the minimum requirement has been met. The Normal state is a cross icon and the Tick state is a tick icon. I use the variable wordCountReached to switch between the two states.
I then add a trigger to change the state of the icon when the variable wordCountReached is changed between True/False. True = Tick (Tick state of icon), False = Cross (Normal state of icon)
I then add the following JavaScript to the "timeline starts on this slide - Execute JavaScript" trigger. This will control the variables in Storyline which will either update text, or cause the icon to switch to the Tick or Cross.
// Get the Storyline Player
const player = GetPlayer();
// Get the text area
const textarea = document.querySelector('textarea');
// Get the number of minimum words required
const minWords = player.GetVar('minWords')
// Add the event listener
textInput = function()
{
const text = textarea.value.trim(); // Get the text and trim whitespace
const words = text.split(/\s+/).filter(word => word.length > 0); // Split text into words
const wordCount = words.length; // Count the words
player.SetVar('wordCountMessage',`You have typed ${wordCount} words. Minimum required is ${minWords}.`);
player.SetVar('wordCountReached',wordCount >= minWords);
}
textarea.addEventListener('input', textInput);
// run to init
textInput();
Hi SamHill
I have an advanced developer who is helping me to limit the characters.
He tried this and other suggestions but it only works for 1 entry box on a slide.
I have multiple slides with up to 3 entry boxes on each slide.
Once done, the user can export the editable PDF. That's why we need the limitations.
Suggestions? Thanks
- SamHill22 days agoSuper Hero
Hi LesB yes, this is for one textarea only. Is you developer advanced in JavaScript? If so, just let them know they need to make adjustments to this code:
const textarea = document.querySelector('textarea');
And change to:
const textarea = document.querySelectorAll('textarea');
And then loop through the results and apply the input listener to the individual textareas.
- LesB21 days agoCommunity Member
Thanks Sam!
Now, I've read that sometimes solutions will work until there's a storyline update. Any other suggestions to mitigate that risk?
- SamHill21 days agoSuper Hero
Hi LesB if the "published" content works, it will continue to work (a Storyline update cannot impact content that is already published). The risk is if there is a Storyline update that changes a structure or attribute you are targeting with JS, the "re-published" content may not work. A textarea will always be a texarea though, and so with regards to the script in this post, the risk is relatively low.
The risk is always there though, and I'm always happy to take it as I know the published content is ok and the risk only comes from future publishing, which you can test, and if found to be broken, adjust the script to fix.
There is the very small, but possible risk of browser updates in future breaking content, but that comes from using deprecated JavaScript. Deprecated JavaScript is given years to phase out from browsers. They key here is not use deprecated JavaScript.
- LesB21 days agoCommunity Member
Hi Sam. Much appreciated. Attached is what the developer did. I pasted it also. What do you think? Thanks
let player = GetPlayer();
// Function to update the character count 1 on a single slide
function updateCharacterCount() {
let text = player.GetVar('text') || ''; // Get the text variable, default to empty string if undefined
let count = text.length; // Calculate the length
player.SetVar('count', count); // Update the count variable
}// Monitor changes in the text variable
setInterval(updateCharacterCount, 100); // Check for changes every 100ms- SamHill21 days agoSuper Hero
I don't think that will work, as the variable value attached to an input field is only updated once the input field loses focus (Storyline functionality), and so I don't think it will pick up a change in length until the user has moved on from the input field.
Also, that interval they are setting won't stop running. It's not an efficient way to handle it. Each time that slide is visited, a new interval will be created. You could end up with multiple intervals running, chewing up resources.
I think the solution I have provided in this post is better.