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.
Welcome to the nuBuilder Forums!
Join our community by registering and logging in.
As a member, you'll get access to exclusive forums, resources, and content available only to registered users.
Join our community by registering and logging in.
As a member, you'll get access to exclusive forums, resources, and content available only to registered users.
Dates
-
- Posts: 503
- Joined: Thu May 24, 2012 2:08 am
- Location: Milan, Italy
- Contact:
Re: Dates
tarelds,
googling around, there are tons of answer: here the first I found.
If you are asking how to integrate your JavaScript in nuBuilder:
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:
When you leave the date-of birth field the age field is populated.
Hope this helps,
Max
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()
Code: Select all
function ageCount() {
...
$('#age-text-field').val(age);
}
in this case, your function must return the age value:
Code: Select all
function ageCount(this.value) {
...
return age;
}
Hope this helps,
Max