The following method was my (primary) objective on this thread. Thanks to all your help, Johan, I think I’ve got something that might even be useful to others, so I thought I’d share.
This phone formatting method can be triggered on data change for any phone field, on any form. Oddly enough, it does work on a tabpanel, but the tabpanels currently in question are showing a form view rather than a list view, so not sure how that would turn out.
The method uses the current field contents, and matches it against a format using ‘#’ as a wild-card. It could easily be altered to other formats, or even have formats passed to the method based on country.
The method will opt out if the current field doesn’t contain numbers, or starts with ‘+’, as international calls do here.
Anything beyond the format is formatted as an extension, either using the descriptive ‘ext’ or whatever that’s in the number, or supplying ‘x’ if not.
Enjoy, and as always, feedback is appreciated.
// gather information about where the user is and what s/he entered
var eleName = application.getMethodTriggerElementName();
var frmName = application.getMethodTriggerFormName();
var fldName = forms[frmName].elements[eleName].getDataProviderID();
var fldContents = forms[frmName].controller.getDataProviderValue(fldName);
// exit if there's nothing in the field to format
if (!utils.stringToNumber(fldContents) || utils.stringLeft(fldContents,1) == '+')
{
return;
}
// Set initial variables.
var entry = fldContents;
var phone = '(###) ###-####';
// first fill in the format pattern
while (utils.stringPatternCount(phone, '#')>0 && entry )
{
if(utils.stringPatternCount('0123456789',utils.stringLeft(entry, 1)))
{
nextPosition = utils.stringPosition(phone, '#', 1, 1 );
phone = utils.stringIndexReplace(phone, nextPosition , 1 , utils.stringLeft(entry, 1));
}
entry = utils.stringRight(entry, entry.length - 1);
}
// if there's more, fill in initial letters beyond the format, such as 'ext' or 'x'
if ( entry )
{
if(utils.stringPatternCount('0123456789',utils.stringLeft(entry, 1)))
{
phone += ' x';
}
else
{
phone += ' ';
}
}
while (entry && !utils.stringPatternCount('0123456789',utils.stringLeft(entry, 1)))
{
phone += utils.stringLeft(entry, 1);
entry = utils.stringRight(entry, entry.length - 1);
}
// if there's still more, enter remaining numbers
if ( entry )
{
phone += ' ';
}
while (entry)
{
if(utils.stringPatternCount('0123456789',utils.stringLeft(entry, 1)))
{
phone += utils.stringLeft(entry, 1);
}
entry = utils.stringRight(entry, entry.length - 1);
}
forms[frmName].foundset[fldName] = phone;