Registration form - Calling the function after filling in the inputs


I’m having trouble creating a custom registration form. There are fields, e.g. firstName, lastName and username. After filling in the firstName and lastName, have to generate and propose a free username, based on the filled in firstName and lastName. How to do it? How can I invoke some java method?
Currently, I have a created a custom .ftl and jar with extenions to this form.