Export (0) Print
Expand All
2 out of 5 rated this helpful - Rate this topic

Field Events: OnChange

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

The OnChange event occurs when the data in a form field has changed and focus is lost. Data in the field is validated before and after the OnChange event.

All fields support the OnChange event.

Note    Although the Status field supports the OnChange event, the field is read-only on the form so the event cannot occur through user interaction. Another script could cause this event to occur by using the FireOnChange method on the field.


The following code example shows how to format basic U.S. telephone numbers. This method supports 7-digit and 10-digit numbers, for example, (410) 555-1212. In the code the expression '"4105551212".length' is equal to 10.

// Get the field that fired the event.
var oField = event.srcElement;

// Validate the field information.
if (typeof(oField) != "undefined" && oField.DataValue != null)
// Remove any non-numeric characters.
  var sTmp = oField.DataValue.replace(/[^0-9]/g, "");

// If the number has a valid length, format the number.
  switch (sTmp.length)
    case "4105551212".length:
      oField.DataValue = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);

    case "5551212".length:
      oField.DataValue = sTmp.substr(0, 3) + "-" + sTmp.substr(3, 4);


You can force the OnChange event to occur for a field from the Microsoft Dynamics CRM form OnLoad event or from the OnChange event of some other field. To do this, use the FireOnChange method.

For example, you can use the following sample script:


© 2010 Microsoft Corporation. All rights reserved.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.