Calculate Age from Date of Birth

Home Forums Quform WordPress Calculate Age from Date of Birth

This topic is: resolved

This topic contains 6 replies, has 2 voices, and was last updated by  justdiveworld 4 months ago.

Viewing 7 posts - 1 through 7 (of 7 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.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

Be inspired. © 2019 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy