Willkommen, Gast
Benutzername: Passwort: Angemeldet bleiben:

THEMA: Validating dates

Validating dates 1 Jahr 3 Monate her #95249

  • StuartMark0
  • StuartMark0s Avatar
  • OFFLINE
  • Junior Lime
  • Beiträge: 26
  • Karma: 0
Howdy,

I have two dates from Date/Time questions, I'd like to ensure respondants to the survey cannot respond with the second of the two dates occuring before the first of the two dates.

i.e. in US date format 02-03-2013, 02-01-2013 would trigger an alert

Is there javascript avaliable which implements this?

Thanks
Der Administrator hat öffentliche Schreibrechte deaktiviert.

Validating dates 1 Jahr 3 Monate her #95259

  • tpartner
  • tpartners Avatar
  • OFFLINE
  • LimeSurvey Team
  • Beiträge: 4058
  • Dank erhalten: 742
  • Karma: 341
Assuming both questions are on the same page, place the following script in the source of the first date question:

<script type="text/javascript" charset="utf-8">
	$(document).ready(function(){
 
		var msg = 'Date 2 cannot be earlier than date 1!';
 
		// Identify the questions
		var q1ID = '{QID}';
		var q1 = $('#question'+q1ID+'');
		var q2 = $(q1).nextAll('div.date:eq(0)'); 
		var q2ID = $(q2).attr('id').split('question')[1];
 
		// Listeners on the date inputs
		$('#question'+q1ID+' input[type="text"], #question'+q2ID+' input[type="text"]').change(function(){
			if($('input[type="text"]', q1).val() != '' && $('input[type="text"]', q2).val() != '') {
				// Build date objects from the input values (format: mm-dd-yyyy)
				var d1Day = Number($('input[type="text"]', q1).val().split('-')[1]);
				var d1Month = Number($('input[type="text"]', q1).val().split('-')[0]) - 1;
				var d1Year = Number($('input[type="text"]', q1).val().split('-')[2]);
				var d1 = new Date(d1Year,d1Month,d1Day);
				var d2Day = Number($('input[type="text"]', q2).val().split('-')[1]);
				var d2Month = Number($('input[type="text"]', q2).val().split('-')[0]) - 1;
				var d2Year = Number($('input[type="text"]', q2).val().split('-')[2]);
				var d2 = new Date(d2Year,d2Month,d2Day);
				// Compare the date objects
				if(d1 > d2) {
					alert(msg);
					$(this).val('');
				}
			}
		});
	});
</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.
Letzte Änderung: 1 Jahr 3 Monate her von tpartner.
Der Administrator hat öffentliche Schreibrechte deaktiviert.
Folgende Benutzer bedankten sich: StuartMark0

Validating dates 1 Jahr 2 Monate her #96262

  • ahchouch
  • ahchouchs Avatar
  • OFFLINE
  • Junior Lime
  • Beiträge: 22
  • Karma: 0
Hi
and if the 02 questions aren't on the same page?
Der Administrator hat öffentliche Schreibrechte deaktiviert.
Moderatoren: ITEd
Ladezeit der Seite: 0.161 Sekunden
Donation Image