Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

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

RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+ 9 months 1 day ago #98141

  • jenllip
  • jenllip's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 11
  • Karma: 0
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>
The administrator has disabled public write access.

RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+ 8 months 3 weeks ago #98365

  • jenllip
  • jenllip's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 11
  • Karma: 0
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!
The administrator has disabled public write access.

RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+ 8 months 3 weeks ago #98366

  • Ben_V
  • Ben_V's Avatar
  • OFFLINE
  • Platinum Lime
  • Posts: 933
  • Thank you received: 193
  • Karma: 62
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

goo.gl/Bw5iM => Recherche GG dans le forum français (remplacer "exemple" dans la barre de recherche)
goo.gl/WX8PH => GG search for english forum (Replace "example" in the search bar)
goo.gl/IxiGu => Búsqueda en el foro en español (Cambiar "ejemplo" en la barra de...
The administrator has disabled public write access.
The following user(s) said Thank You: jenllip

RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+ 8 months 3 weeks ago #98368

  • techpotato
  • techpotato's Avatar
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.
The administrator has disabled public write access.

RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+ 8 months 3 weeks ago #98424

  • jenllip
  • jenllip's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 11
  • Karma: 0
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.
The administrator has disabled public write access.

RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+ 8 months 3 weeks ago #98425

  • Ben_V
  • Ben_V's Avatar
  • OFFLINE
  • Platinum Lime
  • Posts: 933
  • Thank you received: 193
  • Karma: 62
oups... now I don't know because techpotato's intervention sounds good and I can see nothing else !
Benoît

goo.gl/Bw5iM => Recherche GG dans le forum français (remplacer "exemple" dans la barre de recherche)
goo.gl/WX8PH => GG search for english forum (Replace "example" in the search bar)
goo.gl/IxiGu => Búsqueda en el foro en español (Cambiar "ejemplo" en la barra de...
The administrator has disabled public write access.

RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+ 8 months 3 weeks ago #98431

  • techpotato
  • techpotato's Avatar
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.
The administrator has disabled public write access.

RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+ 8 months 3 weeks ago #98455

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 8 months 3 weeks ago by tpartner.
The administrator has disabled public write access.
The following user(s) said Thank You: jenllip

RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+ 8 months 3 weeks ago #98462

  • jenllip
  • jenllip's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 11
  • Karma: 0
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?
The administrator has disabled public write access.

RegEx dependent JS alert on submit: worked in 1.90+, not in 2.00+ 8 months 3 weeks ago #98464

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 3813
  • Thank you received: 683
  • Karma: 328
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
The following user(s) said Thank You: jenllip
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.248 seconds
Donation Image