Create two functions,
one called
formatSSN and
the other called
formatPhoneNo.
Put both functions in a file called “isfprimerlib.js”. As documented in the
ISF Coding and Best Practices,
this file may reside on any directory on the application server beneath the
RequestCenter.war directory; by convention, ISF libraries are placed on a
directory named “isfcode”.
Create a library
reference to your JavaScript file in Script Manager. Be sure to specify the
library in the Libraries tab for a JavaScript function that are attached to
your service.
The resulting code
is:
function getOnlyDigits (inValue)
{
var outValue = '';
var aChar;
for (i=0; i < inValue.length; i++)
{
aChar = inValue.charAt (i);
if ('0' <= aChar && aChar <= '9')
{
outValue = outValue + aChar;
}
}
return outValue;
}
function testValueLength (inValue, inLen, obField, fieldName)
{
if ((inValue.length > inLen) || (inValue.length < inLen))
{
alert (fieldName + ' must have ' + inLen + ' digits and it has ' + inValue.length);
eval('serviceForm.'+obField).setFocus(true);
return false;
}
return true;
}
function formatSSN (obField)
{
var SSNString = getOnlyDigits (eval('serviceForm.'+obField).getValue()[0]);
if (testValueLength (SSNString, 9, obField, 'SSN'))
{
eval('serviceForm.'+obField).setValue([SSNString.slice (0,3) +
'-' + SSNString.slice (3,5) + '-' + SSNString.slice (5)]);
}
}
function formatPhoneNo (obField)
{
var phoneString = getOnlyDigits (eval('serviceForm.'+obField).getValue()[0]);
if (testValueLength (phoneString, 10, obField, 'Phone Number'))
{
eval('serviceForm.'+obField).setValue(['(' + phoneString.slice (0,3) + ') '
+ phoneString.slice (3,6) + '-' + phoneString.slice (6)]);
}
For the field SSN create a function called Customer_SSN_onChange
that calls formatSSN
:
Customer_SSN_onChange ()
{
formatSSN('Customer.SSN');
}
For the PhoneNo
field, create another function called
Customer_PhoneNo_onChange, that calls
formatPhoneNo when the value in the field changes. Just to show an alternative
implementation, these functions pass the name of the field and acts
“by-reference” rather than “by-value”.
Customer_PhoneNo_onChange ()
{
formatPhoneNo ('Customer.PhoneNo');
}
Both functions are
called when the value in its respective field changes.