Welcome to the nuBuilder forums!

Please register and login to view forums and other content only available to registered users.

Dates

Post Reply
tarelds
Posts: 10
Joined: Sat Jan 26, 2013 1:08 pm

Dates

Unread post by tarelds »

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

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

I have tried this script:

JAVA

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;
}

}
HTML
<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.
massiws
Posts: 503
Joined: Thu May 24, 2012 2:08 am
Location: Milan, Italy
Contact:

Re: Dates

Unread post by massiws »

tarelds,
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() {
    ...
    $('#age-text-field').val(age);
}
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,
Max
Post Reply