this is my first post on StackOverflow,
I have read a lot of pages regarding code snippets on this website, and always you give good answers and advice..
Now however I am in need in some assistance..
I want to populate more than one input field, I tried adding a field ID in the JavaScript, but so far no luck.
<script type="text/javascript">
<!--
function calculateAge(inputFieldId, outputFieldId, alert_18){
var age;
var input = document.getElementById(inputFieldId).value;
// Past date info
var pyear = parseInt(input.substring(6,10));
var pmonth = parseInt(input.substring(0,2)) - 1;
var pday = parseInt(input.substring(3,5));
// Today info
today = new Date();
year = today.getFullYear() ;
month = today.getMonth();
day = today.getDate();
if ( month < pmonth ){
age = year - pyear - 1;
}
else if ( month > pmonth ){
age = year - pyear;
}
else if ( month == pmonth ){
if ( day < pday ){
age = year - pyear - 1;
}
else if ( day > pday ){
age = year - pyear;
}
else if ( day == pday ){
age = year - pyear;
}
}
document.getElementById(outputFieldId).value = age;
document.getElementById(outputFieldId).value = age2;
if(alert_18 == 'true'){
if(age < 18){
//Customize alert message
alert('Attention: under 18!');
}
}
}
//-->
</script>
Any help would greatly appreciated
I would suggest the following solution to handle population to an arbitrary number of outputfields (and roughly calculating age – not considering leapyears):
Then when you call it you could either pass a single string as outputfieldid like so:
or your could pass an array of fieldids like so: