CRM2016 introduced the new keypress methods for text and numeric fields to support keypress events on CRM forms. These methods will be useful to provide immediate feedback on key press. Traditionally, all the validations and actions were executed via the “on Change” event. These methods include
-
addOnKeyPress
Use this method to attach an event handler to the keypress event
-
removeOnKeyPress
Use this method to remove an event handler to the keypress event
-
removeOnKeyPress
Use this method to manually fire an event handler to the keypress event
Here are the steps
-
Create a new JScript web resource and add the following code to it.
// JavaScript source code function keyPress() { //attach the validateInput function to the keypress event of the name attribute Xrm.Page.getControl("name").addOnKeyPress(validateInput); } function validateInput() { //get the value of the name field var input = Xrm.Page.getControl("name").getValue(); if (input != "") { //check if the last key pressed is a number if (isNaN(input.substr(input.length - 1)) == false) { //display the message alert("Numeric values are not allowed in the account name"); } } }
-
Save and publish the web resource.
-
Open the account form in customization mode.
-
Add the JScript web resource to the form and call the keypress() method from load.
-
Save and publish the account form.