JavaScript Text Validation with carriage return in Text Entry box

Apr 17, 2014

Hey all, 

I'm trying to build a course that has some text validation.  We're trying to teach a coding language. 

Attached is an example of what I'm trying to do.

In a text input field I want them to type exactly this: 

%%[

var @cat

set @cat = "cat"

]%%

On text input box lose focus, the variable 'BlockInput1' is set to whatever they've entered.

I've tried 2 things, a non-script method and a script method.

Non-script method: 

But, when I set a condition to show the 'correct block' layer once the BlockInput1 variable changes and matches that text, it always shows as incorrect.  I've tried copy/pasting the text into the condition and also using a escape character like '\r' at the new line, but neither work.

Script method: 

I've tried to validate with script as well.  I tried this script to validate, but the script doesn't change the variable at all.  Not even to 'incorrect' always.  Just no change, so it seems I'm going something the script just doesn't like.

Here is the script: 

var p = GetPlayer();

var textValidate = p.GetVar("BlockCode1");

if(textValidate !== "%%[" + String.fromCharCode(13) + "var @cat" + String.fromCharCode(13) + String.fromCharCode(10) + "set @cat = " + String.fromCharCode(34) + "cat" + String.fromCharCode(34) + String.fromCharCode(13) + "var @cat" + String.fromCharCode(13) + String.fromCharCode(10) + "]%%"){

p.SetVar("BlockCode1",incorrect);

}else{

p.SetVar("BlockCode1",correct);

}

worth noting, I've tried this with \r and also without the 'String.fromCharCode(10)'.  String.fromCharCode(10) is line feed, and String.fromCharCode(13) is carriage return.  String.fromCharCode(34) is quotation marks.

I'm assuming it's something to do with the carriage returns since I can validate single line submissions with no issue. Is there some other way to specify that a return is correct in a condition?  At this point I've tried almost everything!  Help!!   

Thanks, 

Sam

2 Replies

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