Home › Forums › Quform WordPress › Calculate Age from Date of Birth
- This topic has 9 replies, 2 voices, and was last updated 4 years, 5 months ago by
amator.
- AuthorPosts
- April 21, 2019 at 3:05 pm #29121
justdiveworld
ParticipantHow can I automatically calculated a person’s age from them selected their date of birth using the calendar element?
April 23, 2019 at 9:52 am #29146Ally
Support StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
April 23, 2019 at 11:42 am #29149justdiveworld
ParticipantHi Ally,
Thanks a million.
I need it in a text field and not hidden field, would you change $hiddenField to $textField to look like the code below?
3_203 is our date field
3_110 is our text fieldI haven’t been able to get it to work:
jQuery(function ($) {
var $dateField = $('.quform-field-3_103'),
$textField = $('.quform-field-3_110'),
datePicker = $dateField.data('kendoDatePicker'),
options = $dateField.data('options') || {},
culture = options.locale || 'en-US',
format;if (window.kendo && datePicker) {
format = options.format ? options.format : kendo.getCulture(options.locale).calendars.standard.patterns.d;$dateField.on('keyup blur', onDateChange);
datePicker.bind('change', onDateChange);
}function onDateChange() {
var value = $dateField.val(),
age = '';if (Quform.isNonEmptyString(value)) {
var date = kendo.parseDate(value, format, culture);if (date !== null) {
age = getAge(date);
}
}$textField.val(age).change();
}function getAge(birthDate) {
var today = new Date(),
age = today.getFullYear() – birthDate.getFullYear(),
m = today.getMonth() – birthDate.getMonth();if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age–;
}return age;
}
});Thanks a million,
Denis
April 23, 2019 at 2:28 pm #29151Ally
Support StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
April 24, 2019 at 9:08 am #29154justdiveworld
ParticipantHi Ally,
I have inserted the code you mentioned, though I am using a text box and not a hidden field. Nonetheless it doesn’t seem to be working.
I have noticed when copying the code into Setting/Custom CSS &JS/Custom JavaScript section that that the code isn’t as well aligned as in your code for some reason. If I paste it into a text file it is aligned.
I have attached a screenshot of it. Not sure if that is an issue, but let me know.
If you need access to this to see, drop me a private message so I can send you access details.
Attachments:
You must be logged in to view attached files.April 24, 2019 at 1:09 pm #29163Ally
Support StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
April 25, 2019 at 3:42 pm #29164justdiveworld
ParticipantThanks Ally,
I was viewing it in preview mode and not on the live site. It works on the live site.
Thanks again for the help.
November 19, 2020 at 2:34 pm #32255amator
ParticipantHi Ally,
What if I need calculate Age in two or more forms? I try with:
$dateField = $(‘.quform-field-1_3’),
$dateField = $(‘.quform-field-3_32’),
$hiddenField = $(‘.quform-field-1_4’),
$hiddenField = $(‘.quform-field-3_33’),but this not work
November 20, 2020 at 11:10 am #32258Ally
Support StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
November 20, 2020 at 12:58 pm #32260amator
ParticipantGreat! Thank you again!
Maybe in the future you can add new field “age”, to be calculated form date form. Or other simple “math” functions
I think a lot of people need this.
Or adding custom js code to each form separately (not one for every form) also might help (for someone who know js). But for all other people simple math should be enough
- AuthorPosts
- You must be logged in to reply to this topic.