In an earlier blog post, we wrote about Form Notifications in CRM 2013. In addition to form notifications, CRM 2013 has also introduced notifications on the field level. Field notifications are very useful when performing custom data validation on a specific field that goes beyond the normal requirements. When your custom data validation logic fails, it will show that the field is invalid and display the error when the mouse hovers over the error icon.

To set notification on a field:
Xrm.Page.getControl(fieldName).setNotification(message);

To clear notification on a field:
Xrm.Page.getControl(fieldName).clearNotification(); 

As an example, let's assume you want to do phone number formatting on the phone number field, and that number is always going to be in the US standard of 10 digits. We will create the function listed below, add it to the Account form, and an an onChange event to the Phone Number field on the Account.

function formatPhone()
{
    var phone = Xrm.Page.getAttribute("telephone1").getValue();
    phone = phone.replace(/[^0-9]/g, '');
    if (phone.length === 10)
    {
        phone = phone.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
        Xrm.Page.getAttribute("telephone1").setValue(phone);
        Xrm.Page.getControl("telephone1").clearNotification();
    }
    else
    {
        Xrm.Page.getControl("telephone1").setNotification("Phone number does not appear to be valid.");
    }
}

This function will get the value of the telephone1 field (Phone Number) check if the number of digits in the phone number is 10. If it isn't, it is not likely to be a valid phone number, and will alert the user using the field notification. When the phone number is valid, the notification set to the field is cleared. The screenshot below shows what the error looks like.

Using field level notifications in conjunction with the form level notifications in CRM 2013 is a powerful new way to perform data validation when fields change and when trying to save the form. Prior to this, you would have to display numerous alert messages or use unsupported customizations to return errors to the users. Now, you can do so using the built-in notification features of CRM 2013.