Requirement: The phone number field should accept only 10 digits and should format in US phone number format (ex: XXX-XXX-XXXX)
This requirement can be completed with some java script:
// Function To validate the Mobile Phone field
function validateMPhone()
{
var phone = Xrm.Page.getAttribute("new_mobilephone").getValue();
var sTmp = phone.replace(/[^0-9]/g, "");
phoneRegex = /^\d{10}$/;
if( !sTmp.match( phoneRegex ) )
{
event.returnValue = false;
alert("Phone must contain 10 numbers.") ;
Xrm.Page.getAttribute('new_mobilephone').setValue('');
}
else
{
var sTmpClean = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
Xrm.Page.getAttribute("new_mobilephone").setValue(sTmpClean);
}
}
Here is the step by step procedure:
1. Create a javascript web resource in the given solution .
2. Click the Text editor button and copy the above code in there.
3. Go to the Field properties of the field you want to validate.
4. Click on "Event" tab.
5. Under 'Form Library' Click on "Add" tab and add the web resource.
6. In the Event Handelers, Select 'OnChange' for the event and then click 'Add'.
7. Add the Function name validateMPhone and click on OK.
8. Publish the Solution.
When you enter a phone number which is not equal to 10 digits you will receive a message box and clicking 'Okay'.
The focus will be in the Phone field and the field would be cleaned up.
No comments:
Post a Comment