718 Washington Ave. N. Suite #101
Minneapolis, MN 55401
View map and all Locations

Send us a message

Microsoft Dynamics CRM 2016 is here, and with it comes some great new features for Keypress including form script support for keypress and several methods for adding, removing, or performing a function when a user presses a key in a control. In today’s blog, we will dive into these new features in more detail. Let’s begin!


Form Script Support for Keypress in Dynamics CRM 2016

One of the new Microsoft Dynamics CRM 2016 features for developers is form script support for keypress. The keypress event is triggered every time a user presses a key in a number or text control. This feature gives developers great flexibility to perform data validation. The form script support for keypress feature overcomes some of the limitations with business rules and the validation performed by JavaScript on the form OnSave and field OnChange events.

In this example, we will show you how a text field can be validated every time a user presses a key in the “Name” field of the Account form. Say you have a scenario where a user cannot name an Account that starts with five consecutive numbers. So an Account name such as 12345MyFavoriteAccount should not be allowed.

The JavaScript code snippet below will perform the following actions:

  • Get the value from the “Name” field on the form
  • Check if the length of the text entered is five
  • If the length is five, check if the content is all numbers
  • If the content is all numbers, display an alert message
function checkAccountName() {

var nameKeyPressFunction = function (ext) {

//Get the value from the name field on the form

var accountName = Xrm.Page.getControl(“name”).getValue();

//Check if length is 5

if (accountName.length == 5) {

//Check if the accountName only contains numbers

if (isNaN(accountName) == false) {

alert(“I don’t think ‘” + accountName + “‘ is a good idea for an Account name!”);






Nothing new here so far! Now let’s take a look at how to wire the code above to be executed every time a user presses a key in the “Name” field on the Account form.

1. First, create a web resource containing the JavaScript code above.

2. Save and Publish the web resource.

Now it all comes together! Follow the steps below to wire the JavaScript code to the keypress event of the “Name” field:

1. Open the Account form.

2. Click on Form Properties.

3. Make sure you are on the Events tab.

4. Add the web resource created previously as a Form Library.

5. Add an Event Handler for the checkAccountName function.

6. Click on the OK button.

7. Save and Publish the form.

Now, the let’s test this exciting new functionality. Open the form to create a new Account and type a few numbers into the “Name” field. You should see a message pop up letting you know that the name typed is invalid.

Keypress Methods in CRM 2016

The CRM 2016 SDK also now includes several methods for adding, removing, or performing a function when a user presses a key in a control. Now, you can use addOnKeyPress, removeOnKeyPress, and fireOnKeyPress methods to get immediate feedback or take action while a user types in a control. These methods let you perform data validations in a control even before the user saves the value in a form.

*Note: these methods are not supported for CRM mobile clients (phones or tablets).

1. addOnKeyPress

You can use the addOnKeyPress method to add a function as an event handler for the keypress event so that the function is called when you type a character in a specific text or number field.

For example, here is the JavaScript code that uses the addOnKeyPress method to configure the auto-completion experience. For more information on this method, check out the article Sample: Auto-complete in CRM controls.


Type: function reference

Remarks: The function will be added to the bottom of the event handler pipeline. The execution context is automatically set to be passed as the first parameter passed to the event handler set using this method. For more information, check out the article Execution context (client-side reference).

You should use a reference to a named function rather than an anonymous function if you want to remove the event handler for the field at a later date.

2. removeOnKeyPress

Use the removeOnKeyPress method to remove an event handler for a text or number field that you added using addOnKeyPress.


Type: function reference

Remarks: If an anonymous function is set using addOnKeyPress, it cannot be removed using this method.

3. fireOnKeyPress

Use the fireOnKeyPress method to manually fire an event handler that you created for a specific text or number field to be executed on the keypress event.

That’s it! To stay up to date on everything new with CRM 2016, make sure you subscribe to our blog and check out some of PowerObjects’ awesome webinars, where we delve into all that’s new and exciting with Dynamics CRM.

Happy CRM’ing!

Avatar for JoeCRM


Joe CRM is a CRM superhero who runs on pure Microsoft Dynamics CRM adrenaline. As the face of PowerObjects, Joe CRM’s mission is to reveal innovative ways to use Dynamics CRM and bring the application to more businesses and organizations around the world.