Calculate Age from Date of Birth

Home Forums Quform WordPress Calculate Age from Date of Birth

This topic is: resolved
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #29121
    justdiveworld
    Participant

    How can I automatically calculated a person’s age from them selected their date of birth using the calendar element?

    #29146
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #29149
    justdiveworld
    Participant

    Hi 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 field

    I 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

    #29151
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #29154
    justdiveworld
    Participant

    Hi 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.
    #29163
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #29164
    justdiveworld
    Participant

    Thanks 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.

    #32255
    amator
    Participant

    Hi 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 🙂

    #32258
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #32260
    amator
    Participant

    Great! 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

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.
Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy