RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+

More
4 years 4 months ago #98141 by jenllip
jenllip created the topic: RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+
The following JS added to the question text in my v1.90+ survey worked properly, but is not working in v2.00+ when I imported the survey (and changed the SGQ identifiers appropriately).

I tried adding a space after the opening { and before the closing } in the regexes, per changes with the new EM, but it still didn't working right and I can't figure out why.

When someone clicks next/submit, this script should check the word count of the answer.
- If <75, pop up alert "too short" and do not proceed to next question so respondent can add to response as written.
- If 75-150 words, no alert and just continue to next question.
- If >150 words, pop up alert "this is long, but ok. Don't do it again" and continue to next question.

Any help or suggestions for alternative methods would be greatly appreciated!
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
 
// Interrupt next/submit function and check for unanswered first row of array
$('form#limesurvey').submit(function(){
 
var sID = 41668;
var gID = 50;        
var qID = 201;
 
var warning1 = 'The story you wrote is just a few sentences short of optimum length for the exercise and we will have difficulty accurately interpreting it. Please simply add two or three more sentences to this story as you have written it so far.';
 
var warning2 = 'Your story has been submitted. Remember, keep your stories brief: 5-6 minutes or 75 to 125 words. The size of the text box is approximately 90 words.';
 
var pattern1 = /^\s*\S+(?:\s+\S+){74,}\s*$/;
 
var pattern2 = /^\s*\S+(?:\s+\S+){75,150}\s*$/;
 
var answer1 = $('#answer'+sID+'X'+gID+'X'+qID+'').val();
 
 
           if (answer1 && pattern1.test(answer1) != true) {
                                alert (warning1);
                                                return false;
            }
            else {
                if (answer1 = true && pattern2.test(answer1) != true) {
                                alert (warning2);
                                                return true;
                }
                else {                
                        return true;
                }
            }
 
        });
    });
 
</script>

Please Log in to join the conversation.

More
4 years 3 months ago #98365 by jenllip
jenllip replied the topic: RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+
I hate to bump this up, but I'm getting a bit desperate. Does anyone have thoughts on what changed between 1.90+ and 2.00+ that would have caused this js to break? Many thanks!

Please Log in to join the conversation.

More
4 years 3 months ago #98366 by Ben_V
Ben_V replied the topic: RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+
Hello,

The big change is called 'Expression Manager'...

A big probability is that your issue is related to:
" Anything surrounded by curly braces is an Expression, with two exceptions: If there is whitespace after the opening brace or before the closing brace, it is ignored. "

For example for your following regex, '{74}' may be parsed and misinterpreted:
var pattern1 = /^\s*\S+(?:\s+\S+){74,}\s*$/;

Check curly braces, and try to insert whitespaces every where needed as described in the manual...
e.g. { 74,} ... but I don't know if a regex will be correctly working this way

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
The following user(s) said Thank You: jenllip

Please Log in to join the conversation.

  • techpotato
  • techpotato's Avatar
  • Visitor
  • Visitor
4 years 3 months ago #98368 by techpotato
techpotato replied the topic: RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+
You will need to change your regular expressions to strings, and use "\x7b" instead of "{".

The issue is described in the workaround scripts section of the wiki.

You could define something like "var openbrace='\x7b';" then concatenate that into whatever strings you need if you feel that readability is more of an issue.

Please Log in to join the conversation.

More
4 years 3 months ago #98424 by jenllip
jenllip replied the topic: RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+
Thank you so much, I knew it was something to do with the { and spaces, but couldn't figure out how to fix it. Unfortunately, I'm still not getting something.

These do not trigger any warning at all:
var pattern1 = "/^\s*\S+(?:\s+\S+)\x7b74,}\s*$/";
var pattern1 = "/^\s*\S+(?:\s+\S+){ 74,}\s*$/";
var patternpart1 = "/^\s*\S+(?:\s+\S+)";
var pattern1part2 = "74,}\s*$/";
var pattern1 = patternpart1+'\x7b'+pattern1part2;

These trigger a warning every time, regardless of pattern match. Also after the warning is clicked through, it requires a manual page refresh (which deletes the text entered so far) in order to activate the next button again, leading me to believe there's some problem with that interrupt submit function:
var pattern1 = /^\s*\S+(?:\s+\S+)\x7b74,}\s*$/;
var pattern1 = /^\s*\S+(?:\s+\S+){ 74,}\s*$/;

Sorry for sounding so dense, I really don't know javascript and am just trying to hack this together.

Please Log in to join the conversation.

More
4 years 3 months ago #98425 by Ben_V
Ben_V replied the topic: RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+
oups... now I don't know because techpotato's intervention sounds good and I can see nothing else !

Benoît

EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)

Please Log in to join the conversation.

  • techpotato
  • techpotato's Avatar
  • Visitor
  • Visitor
4 years 3 months ago #98431 by techpotato
techpotato replied the topic: RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+
You can only use character entities in a string not in a direct regexp declaration, so use the constructor form instead:

var pattern1 = new RegExp( "^\s*\S+(?:\s+\S+)\x7b74,}\s*$");

I hope that helps.

Please Log in to join the conversation.

More
4 years 3 months ago - 4 years 3 months ago #98455 by tpartner
tpartner replied the topic: RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+
Another approach may be to create an array of the words in the textarea (using spaces as delimiters) and then count the items in the array:

<script type="text/javascript" charset="utf-8">	
	$(document).ready(function(){
 
		$('form#limesurvey').submit(function(){
 
			var sID = 41668;
			var gID = 50;        
			var qID = 201;
 
			var warning1 = 'The story you wrote is just a few sentences short of optimum length for the exercise and we will have difficulty accurately interpreting it. Please simply add two or three more sentences to this story as you have written it so far.';
 
			var warning2 = 'Your story has been submitted. Remember, keep your stories brief: 5-6 minutes or 75 to 125 words. The size of the text box is approximately 90 words.';
 
			var wordCount = $('#answer'+sID+'X'+gID+'X'+qID+'').val().split(' ').length;
 
			if (wordCount < 75) {
				alert (warning1);
				return false;
			}
			else {
				if (wordCount > 150) {
					alert (warning2);
					return true;
				}
				else {                
					return true;
				}
			}
 
		});
    });
</script>

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 4 years 3 months ago by tpartner.
The following user(s) said Thank You: jenllip

Please Log in to join the conversation.

More
4 years 3 months ago #98462 by jenllip
jenllip replied the topic: RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+
tpartner, this is such a perfect solution, I could kiss you.

I'm still having the problem with the submit function though. When the word count is less than 75 and the alert pops up and interrupts the submit, it deactivates the next button so I have to manually refresh the page (and lose what was written so far!) in order to reactivate the next button to try submitting again. This was not the behavior in the last version of ls. Any thoughts on ls changes that would have affected that?

Please Log in to join the conversation.

More
4 years 3 months ago #98464 by tpartner
tpartner replied the topic: RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+
Oh, yeah, that's because of a new "feature".

Try this:

<script type="text/javascript" charset="utf-8">	
	$(document).ready(function(){
 
		// Interrupt next/submit function and check for unanswered first row of array
		$('form#limesurvey').submit(function(){
 
			// Override the built-in "disable navigation buttons" feature
			$('#moveprevbtn, #movenextbtn, #movesubmitbtn').attr('disabled', '');
 
			var sID = 41668;
			var gID = 50;        
			var qID = 201;
 
			var warning1 = 'The story you wrote is just a few sentences short of optimum length for the exercise and we will have difficulty accurately interpreting it. Please simply add two or three more sentences to this story as you have written it so far.';
 
			var warning2 = 'Your story has been submitted. Remember, keep your stories brief: 5-6 minutes or 75 to 125 words. The size of the text box is approximately 90 words.';
 
			var wordCount = $('#answer'+sID+'X'+gID+'X'+qID+'').val().split(' ').length;
 
			if (wordCount < 75) {
				alert (warning1);
				return false;
			}
			else {
				if (wordCount > 150) {
					alert (warning2);
					return true;
				}
				else {                
					return true;
				}
			}
 
		});
    });
</script>

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: jenllip

Please Log in to join the conversation.

More
4 years 3 months ago #98467 by jenllip
jenllip replied the topic: RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+
Oh those "features." They'll get you every time. Out of curiosity, what was the new change in ls?

This worked perfectly; exactly the same as the old script in 1.90 did. I hope this will be useful to any others encountering weird upgrade issues!

Please Log in to join the conversation.

More
4 years 3 months ago #98469 by tpartner
tpartner replied the topic: RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+
The new feature is to disable the "Next" button after clicking to prevent double-clicks.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!