Hi,
As Mayank has suggested it would be valuable and I would say a best practice to reuse your code here. You can use a ScriptObject, and write your function there and call it from each of the fields that use it, and you can further extend this to be used by other forms by creating a scriptObject fragment to be stored in your fragment library.
Script Objects and Fragments are discussed and documented here in these Designer help documents:
https://helpx.adobe.com/content/dam/help/en/experience-manager/6-4/forms/pdf/scripting-reference.pdf
https://helpx.adobe.com/content/dam/help/en/experience-manager/6-4/forms/pdf/using-designer.pdf
Script fragments
A script fragment contains reusable JavaScript functions or values that are stored separately from
a particular object, such as a date parser or a web service invocation. These fragments include a
single script object that appears as a child of variables in the Hierarchy palette. Fragments
cannot be created from scripts that are properties of other objects, such as event scripts like
validate, calculate, or initialize.
For more information, see UsingScript Fragments.
RELATED LINKS:
Fragment Library palette menu Binding fragments to a data source
Using choice subform sets