Unread postby tarelds » Thu Apr 10, 2014 3:44 pm

I wonder if anyone has worked out some code to work out an age based on of birth.

Two text fields.One for Date of Birth.One for the Age in years.

I have tried this script:


function ageCount() {
var date1 = new Date();
var dob= document.getElementById("date_of_birth").value;
var date2=new Date(dob);
var pattern = /^\d{1,2}\/\d{1,2}\/\d{4}$/; //Regex to validate date format (dd/mm/yyyy)
if (pattern.test(dob)) {
var y1 = date1.getFullYear(); //getting current year
var y2 = date2.getFullYear(); //getting dob year
var age = y1 - y2; //calculating age
birth.value=(age) + 2;
return true;
} else {
alert("Invalid date format. Please Input in (dd/mm/yyyy) format!");
return false;

<input type="text" name="date_of_birth" id="date_of_birth" />
<input type="submit" value="Age" onClick="ageCount();">

But still cannot get it to put the age into a text field.

I hope it is OK to ask such questions in this forum.
Posts: 10
Joined: Sat Jan 26, 2013 9:38 pm

Re: Dates

Unread postby massiws » Fri Apr 11, 2014 11:59 pm

googling around, there are tons of answer: here the first I found.

If you are asking how to integrate your javascript in nuBuilder:
  • add your function in Custom Code -> Javascript;
  • add a Button object on your form;
  • in the Event tab of the Button object insert a new event: Event Name => onclick - Javascript => ageCount()
Obviously, your function must set the result in your age-text-field:
Code: Select all
function ageCount() {

Another way, without using a Button object, could be insert the event in date-of-birth text object -> Event: Event Name => onblur - Javascript => $( '#age-text-field-id' ).val( ageCount( this.value ) );
in this case, your function must return the age value:
Code: Select all
function ageCount(this.value) {
    return age;

When you leave the date-of birth field the age field is populated.

Hope this helps,
Posts: 503
Joined: Thu May 24, 2012 9:38 am
Location: Milan, Italy

Return to Custom Code

Who is online

Users browsing this forum: No registered users and 3 guests