Adding a Mask to an Xdialog Field

Description

If you would like to add a mask that formats a field, the following script provides a template. The mask is applied when the user changes focus from (or leaves) the text field.

Examples 

Set the phone field event to pass all events, not just the change event.

dim i as N
dim char as C
dim temp as C
ui_dlg_box("Mask Example",<<%dlg%
;
Telephone Number |[.20phone!phone_event_*]

The OK button closes the dialog box.

;
%dlg%, <<%code%
if a_dlg_button = "OK" then
    ' exit
end if

When the phone field loses focus, strip out the non-numeric characters.

if a_dlg_button = "phone_event_killfocus" then
    char = ""
    for i = 1 to len(phone)
        char = substr(phone,i,1)
        if char >= "0" .and. char <= "9"
            temp = temp + char
        end if
    next i

This part is totally customizable. I decided that the phone number had to be at least 7 characters long and that the area code would be surrounded by parentheses. The local exchange number would be separated from the house number by a hyphen.

If len(temp) >= 7 .and. len(temp) <= 10
        phone = "(" + left(temp,3) + ")" + substr(temp, 4, 3) + "-" + substr(temp, 7, 4)
    end if
    If len(temp) > 10
        phone = phone + " x" + right(temp, len(temp) - 10)
    end if
    ui_dlg_refresh("Mask Example")
    a_dlg_button = ""
else
    a_dlg_button = ""
end if
%code%)

See Also