Last active
January 8, 2023 22:27
-
-
Save alex-authlab/cba0340b517362bc428d4a918d8ce699 to your computer and use it in GitHub Desktop.
Birthday Calculation in Fluent Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Age calculator | |
// 1. First take a Date picker & a text input field | |
// 2. In the text input element add the class 'age' | |
// 3. Add a custom class 'date' in the date picker ,then add the following js in your fluent form custom js | |
jQuery( ".date" ).change(function() { | |
var age = getAge(jQuery(this).val()) | |
jQuery('.age').val( age) | |
}); | |
function getAge(DOB) { | |
//when date format dd/mm/yyyy | |
birthDate = DOB.split("/"); | |
currentDate = new Date(); | |
// javascript month counts from 0, so add +1 | |
var years = (currentDate.getFullYear() - birthDate[2]); | |
if (currentDate.getMonth()+1 < parseInt(birthDate[1]) || currentDate.getMonth()+1 == parseInt(birthDate[1]) && currentDate.getDate() < birthDate[0]) { | |
years--; | |
} | |
if(years < 0){ | |
years = 0; | |
} | |
return years; | |
} |
Its showing NaN in text box....any idea why ???
and also, var age = getAge(jQuery(this).val()) it's showing semi column missing at this line
You may want to check line 21 whether you split by the right character, depending on the date format chosen. I had dots, so I had to adjust it to birthDate = DOB.split(".");
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, there is an error in this script. The correct version that works for me is this one:
// Age calculator