how to display the age automatically

More
4 years 4 months ago #90005 by mimi
mimi created the topic: how to display the age automatically
hi...

i have two question here...

1)I would like to know how to display the age automatically if ic num has been added to the system. can lime survey do it?

2)and how about if i want to put the array type question here but with different number of arrays. here i have attached the view of question that i meant for the second question.

hope there who are willing to help.

thank you.
Attachments:

Please Log in to join the conversation.

More
4 years 4 months ago #90007 by Ben_V
Ben_V replied the topic: how to display the age automatically
Hello,

1) for the less, it's possible from date of birth
No idea how to adapt this for ic num.
Ben

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.

More
4 years 4 months ago #90009 by tpartner
tpartner replied the topic: how to display the age automatically
1) What is an "ic num"?

2) You can hide the unwanted radio buttons with CSS. Use a tool like Firebug for Firefox to identify them.

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
4 years 4 months ago #90013 by Ben_V
Ben_V replied the topic: how to display the age automatically
probably "identity card number" :)

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.

More
4 years 4 months ago #90044 by mimi
mimi replied the topic: how to display the age automatically
yes... its identify card number. tQ mr.ben :)

aha...i c..tQ mr.tpartner.

Please Log in to join the conversation.

More
4 years 4 months ago #90094 by tpartner
tpartner replied the topic: how to display the age automatically
Can you give us an example of the IC number format? (we don't use them here)

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
4 years 4 months ago #90326 by mimi
mimi replied the topic: how to display the age automatically
910110-10-5555

910110-based on the day of birth. it means the person birth on 10/01/1991
10-based on the place of birth.means the country.
5555-this represents generic serial numbers. The odd numbers for the last num denotes male while the even numbers denote female

tQ

Please Log in to join the conversation.

More
4 years 4 months ago #90351 by tpartner
tpartner replied the topic: how to display the age automatically
I'm still not clear on the date format. Is "910110" in the YYMMDD or YYDDMM format (your example could be either).

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
4 years 4 months ago #90416 by mimi
mimi replied the topic: how to display the age automatically
its already my own example.huhu. the num that i give is just only an example. it can be any num. and 910110 is YYMMDD.

Please Log in to join the conversation.

More
4 years 4 months ago #90470 by tpartner
tpartner replied the topic: how to display the age automatically
Okay, if you have two text inputs on a page, the first being the IC input and the second being the age:

1) Add the following to the end of template.js:
function icToAge() {
 
	// A listener on the first input
	$('input.text:eq(0)').bind('input propertychange', function() {
 
		// Define the IC number format yymmdd-##-####
		var patternMatch = /^[0-9]{2}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])-[0-9]{2}-[0-9]{4}$/;
 
		var icInput = $(this).val();
 
		if (patternMatch.test(icInput) != true) {
			// Invalid IC number format
			$('input.text:eq(1)').val('No age available.');
		}
		else {
			var d = new Date();
			var todayYear = d.getFullYear();
			var todayShortYear = String(todayYear).substr(2,2); 
			var todayMonth = d.getMonth()+1; 
			var todayDate = d.getDate();
 
			var birthDateCode = icInput.split('-')[0];
			var birthYear = birthDateCode.substr(0,2);
			var birthMonth = birthDateCode.substr(2,2);
			var birthDate = birthDateCode.substr(4,2);
			if(birthYear > todayShortYear) {
				birthYear = 19+birthYear;
			}
			else {
				birthYear = 20+birthYear;
			}
 
			var age = todayYear - birthYear;
 
			if ( todayMonth < birthMonth) {
				age--;
			}
			else if ((birthMonth == todayMonth) && (todayDate < birthDate)) {
				age--;
			}
 
			// Load the age into the second input
			$('input.text:eq(1)').val(age);
		}
 
	});
}

2) Add this to the source of the IC input question to call the icToAge() function:
<script type="text/javascript" charset="utf-8">
	$(document).ready(function() {
		icToAge();
	});
</script>

Some notes:
- The icToAge() function validates an IC input against a regular expression and if it passes, the current age is calculated and inserted in the next text input.
- This code is designed for a page where the IC input is the first text input and the age is the second.
- The function resides in template.js because Expression Manager destroys valid regular expressions if placed in the question source.
- The validation regex could probably use some tuning for 30-day-months, etc - I just did it quickly for this example.

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
4 years 3 months ago #90985 by mimi
mimi replied the topic: how to display the age automatically
Aha. Thank You so much for helping.

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now