Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8739967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:02:55+00:00 2026-06-13T11:02:55+00:00

this is my first post on StackOverflow, I have read a lot of pages

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:02:56+00:00Added an answer on June 13, 2026 at 11:02 am

    I would suggest the following solution to handle population to an arbitrary number of outputfields (and roughly calculating age – not considering leapyears):

    <script type="text/javascript">
    <!--
    function calculateAge(inputFieldId, outputFieldId, alert_18){
      var input = document.getElementById(inputFieldId).value,
          pyear = parseInt(input.substring(6,10)),
          pmonth = parseInt(input.substring(0,2)) - 1,
          pday = parseInt(input.substring(3,5)),
          dt1 = new Date(),
          dt2 = new Date(pyear, pmonth, pday),
          ageMs = dt2.getTime() - dt1.getTime(),
          age = parseInt(ageMs / 1000 / 60 / 60 / 24 / 365);
    
    
      if(outputFieldId instanceof Array) {
        for(var i = 0, l = outputFieldId.length; i < l; i++)
          document.getElementById(outputFieldId[i]).value = age;
      }
      else
        document.getElementById(outputFieldId).value = age; 
    
      if(alert_18 && age < 18) {
        //Customize alert message
    
        alert('Attention: under 18!');
      }
    }
    //-->
    </script>
    

    Then when you call it you could either pass a single string as outputfieldid like so:

    calculateAge("get-age", "calculated-age", true);
    

    or your could pass an array of fieldids like so:

    calculateAge("get-age", ["calculated-age1", "calculated-age2"], true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, I must say that I have read several post about this at StackOverflow
This is my first post on StackOverflow! I have a background service running and
This is my first post on StackOverflow, so please be gentle... I have some
This is my first post on stackoverflow, so please excuse me if my question
This is my first post in stackoverflow. I want to improve its google pagerank
this is my first post here on stackoverflow and am very impressed by the
This is my first post and I my first experience with jquery. I have
This is my first post here. I have a problem. I need to take
This is my first post to stackoverflow, so bear with me. :) I am
This is my first post on stackoverflow, hello everyone! My first venture into jQuery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.