Community > Answers
31 views

Update a field using javascript

Hello

I need some help. I have a liveform for registration  that collects birthdate. I would like to use a javascript function to convert the birthdate to age that updates a custom profile field called age.  Here is what I did

1. created a hidden field called Age that  corresponds to a custom profile field called 

2. Under Actions using On Form View I selected Bind or update a single form field via javascript

3. For field name I selected the Age field

4. Not sure where the token value is used but I input token agecalc

5. In the javascript I used the following but am not sure now the token value I specified is used

import java.time.Period;
public class AgeCalculator {
    public static void main(String[] args) {
        //  (using liveform birthdate token)
        String birthdateStr = "[birthdate]";

        // Parse the birthdate string to LocalDate
        LocalDate birthdate = LocalDate.parse(birthdateStr);

        // Calculate the age
        int age = calculateAge(birthdate);

        System.out.println("Age: " + age);
    }

    public static int calculateAge(LocalDate birthdate) {
        LocalDate currentDate = LocalDate.now();

        // Calculate the difference between birthdate and current date
        Period period = Period.between(birthdate, currentDate);

        // Extract the years from the Period
        return period.getYears();
    }
}

Any help would be greatly appreciated.

Thanks

carl carl
asked 08/21/2023 23:15
Add Comment
Gagandeep Singh
replied 08/22/2023 09:23

It is not possible at this time.

Last Activity 08/22/2023 09:23

No answers found

Add an Answer