A while back, I posted a phone formatting method that was working quite well for me. It could be set as an onDataChange method for any phone field, and it would format the phone and put the data back in the right field.
Now it’s not working, and I can’t figure out why. At first, I thought there’d been a change in 2.1.2, but that’s seeming less and less likely.
From running debugger, it seems that the variable fldContents isn’t getting defined/populated. Can anyone else reproduce this? Better yet, does someone still have 2.1.1 that they can try this on, to confirm it’s not a version difference? The method doesn’t require any special field definitions, just an onDataChange trigger on any existing phone field (text).
// 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; // *****This is where the method is currently exiting*****
}
// 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;