I believe the following code might be what you have in mind.
Below is an older example of code from HP for some custom workflow code. The code is pretty simple so should be too hard at all to adapt to your setup.
The below features BG_USER_03 for the UDF in question, but you can obviously put in whatever UDF you are working with.
In the Bug_FieldChange subroutine, it checks whether the user types in between 15-30 characters. If not, it returns an error message and resets the value to what it originally was. The LastValue function is a utility function that returns the original value of the field before any changes were made.
Sub Bug_FieldChange (FieldName)
' Check the length of the string that the user types in BG_USER_03.
' If the length is not between 15 to 30 characters, return an error message and
' switch back the value to what it originally was.
if FieldName = ''BG_USER_03'' and (Len(Fields(''BG_USER_03'').Value)<15 or Len(Fields(''BG_USER_03'').Value)>30) then
MsgBox ''This field should be between 15 to 30 characters long!''
Fields(''BG_USER_03'').Value = LastValue(''BG_USER_03'')
'This function returns the value of the field before the user changes it.
'FieldName - name of field you are looking for
'If the bug is new then the previous value is Null
if fields(''BG_BUG_ID'').IsNull then LastValue =''''
set td = TDConnection
set com = td.Command
com.CommandText = ''select '' & FieldName & '' from BUG where BG_BUG_ID='' &Fields(''BG_BUG_ID'').Value
set rec = com.Execute
LastValue = rec.FieldValue(Cstr(FieldName))
set com = nothing
set rec = nothing