Welcome, Guest
Username: Password: Remember me

TOPIC: Regex test in multiple short text

Regex test in multiple short text 3 years 13 hours ago #69960

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
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 |
The administrator has disabled public write access.

Re: Regex test in multiple short text 3 years 9 hours ago #69966

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4372
  • Thank you received: 830
  • Karma: 381
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 3 years 9 hours ago by tpartner.
The administrator has disabled public write access.

Re: Regex test in multiple short text 2 years 11 months ago #69978

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
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 |
The administrator has disabled public write access.

Re: Regex test in multiple short text 2 years 11 months ago #69989

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4372
  • Thank you received: 830
  • Karma: 381
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Regex test in multiple short text 2 years 11 months ago #70007

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5426
  • Thank you received: 307
  • Karma: 252
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)limesurvey.org'"
The administrator has disabled public write access.

Re: Regex test in multiple short text 2 years 11 months ago #70026

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
OK, will do
Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
The administrator has disabled public write access.

Re: Regex test in multiple short text 2 years 11 months ago #70063

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5426
  • Thank you received: 307
  • Karma: 252
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)limesurvey.org'"
The administrator has disabled public write access.

Re: Regex test in multiple short text 2 years 11 months ago #70143

  • waitz
  • waitz's Avatar
  • OFFLINE
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
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: 2 years 11 months ago by waitz.
The administrator has disabled public write access.
The following user(s) said Thank You: DenisChenu

Re: Regex test in multiple short text 2 years 11 months ago #70154

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4372
  • Thank you received: 830
  • Karma: 381
Looks good. :cheer:
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.

Re: Regex test in multiple short text 2 years 11 months ago #70169

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5426
  • Thank you received: 307
  • Karma: 252
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)limesurvey.org'"
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.228 seconds
Donation Image