Check out the LimeSurvey source code on GitHub!

Regex test in multiple short text

More
5 years 2 months ago #69960 by waitz
I have a few subquestions in a multiple short text question, and one of them is for an email address.

I would like to add a regex test to this subquestion only, that checks if it is a valid email address:
^[a-zA-Z0-9\_\-]+[a-zA-Z0-9\.\_\-]*@([a-zA-Z0-9\_\-]+\.)+([a-zA-Z]{2,4}|travel|museum)$
From: www.regexlib.com/REDetails.aspx?regexp_id=1653

How can I do it...?

Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |

Please Log in to join the conversation.

More
5 years 2 months ago - 5 years 2 months ago #69966 by tpartner
Add the following code to the source of your question.

Replace "QQ" with the question ID and "NN" with the row number that you want to validate.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// Call the email function with the question ID and the row number
		emailTest(QQ, NN);
 
		function emailTest(qID, inputNum) {
 
			// Interrupt next/submit function 
			$('#movenextbtn, #movesubmitbtn').click(function(){
 
				// Some vars - modify as required
				var emailMatch = /^[a-zA-Z0-9\_\-]+[a-zA-Z0-9\.\_\-]*@([a-zA-Z0-9\_\-]+\.)+([a-zA-Z]{2,4}|travel|museum)$/;
				var msg1 = 'Please enter a valid email address.';
 
				// Test the input
				var emailInput = $('#question'+qID+' li:eq('+(inputNum-1)+') input.text').val();
				if(emailInput != '' && !emailMatch.test(emailInput)) {
					alert(msg1);
					$('#question'+qID+' li:eq('+(inputNum-1)+') input.text').css({'background':'pink'});
					return false;
				}
				else {
					$('#question'+qID+' li:eq('+(inputNum-1)+') input.text').css({'background':''});
					return true;
				}						
			});
		}
	});
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 5 years 2 months ago by tpartner.

Please Log in to join the conversation.

More
5 years 2 months ago #69978 by waitz
You're the best, Tony ;)
You should seriously consider cloning yourself.

And for the record (if anyone else will search this thread.) I replaced "email" with "phone" for a another field that needs a phone number with international country code:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// Call the phone function with the question ID and the row number
		phoneTest(QQ, NN);
 
		function phoneTest(qID, inputNum) {
 
			// Interrupt next/submit function 
			$('#movenextbtn, #movesubmitbtn').click(function(){
 
				// Some vars - modify as required
				var phoneMatch = /^[0-9,+,(), ,]{1,}(,[0-9]+){0,}$/;
				var msg1 = 'Please enter a valid phone number.';
 
				// Test the input
				var phoneInput = $('#question'+qID+' li:eq('+(inputNum-1)+') input.text').val();
				if(phoneInput != '' && !phoneMatch.test(phoneInput)) {
					alert(msg1);
					$('#question'+qID+' li:eq('+(inputNum-1)+') input.text').css({'background':'pink'});
					return false;
				}
				else {
					$('#question'+qID+' li:eq('+(inputNum-1)+') input.text').css({'background':''});
					return true;
				}						
			});
		}
	});
 
</script>

Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |

Please Log in to join the conversation.

More
5 years 2 months ago #69989 by tpartner

You should seriously consider cloning yourself.

Too funny! I already have an identical twin.:)

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.

More
5 years 2 months ago #70007 by Mazi
waitz, can you please add you solution at manual -> workarounds ->JavaScript and maybe afterwards also link there from docs.limesurvey.org/Using+regular+expres...tions+for+LimeSurvey

Thanks!


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
5 years 2 months ago #70026 by waitz
OK, will do

Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |

Please Log in to join the conversation.

More
5 years 2 months ago #70063 by Mazi
Thanks, please post the link here afterwards.


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
5 years 2 months ago - 5 years 2 months ago #70143 by waitz
Cool, my first wiki entry ever (let me know if I screwed something up ;) )

Check validity of international phone number in subquestion
and
[url=
docs.limesurvey.org/tiki-index.php?page_...in_subquestion]Check validity of email address in subquestion[/url]

Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
Last Edit: 5 years 2 months ago by waitz.
The following user(s) said Thank You: DenisChenu

Please Log in to join the conversation.

More
5 years 2 months ago #70154 by tpartner
Looks good. :cheer:

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.

More
5 years 2 months ago #70169 by Mazi
Great work, feel free to add further solutions you are using.


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form