Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Get keycode using onkeypress event in Dynamics CRM

(0) ShareShare
ReportReport
Posted on by 3,614

In one of field on contact my requirements are to allow the user to enter only digits and if any other key such as alphabetic or special symbol key is pressed it should alert about the invalid key. I tried with this but I cannot find the way to pass pressed key as parameter.

Any idea to achieve this.

*This post is locked for comments

  • Verified answer
    RE: Get keycode using onkeypress event in Dynamics CRM

    Hi

    The Xrm framework does not provide the raw event object, this information is cut in the call stack by design.

    You can try to obtain the raw event from the global content, but that may change depending on the forms engine enabled due it was enhanced in the 2016 version... accordingly to your code, it may be something like:

    function checkNumber(fieldName) {
       try {
           debugger;
           var nameKeyPressFunction = function (exContent) {
               var e = event || window.event || parent.event;
               var key = e.keyCode;
               console.log(key);
           }
           Xrm.Page.getControl(fieldName).addOnKeyPress(nameKeyPressFunction);
       } catch (e) {
          console.log(e);
       }
    }


    Mark as answer if this solves your issue.

    Best regards

  • Suggested answer
    Aiden Kaskela Profile Picture
    Aiden Kaskela 19,694 on at
    RE: Get keycode using onkeypress event in Dynamics CRM

    Hi Mohsin,

    There's no 'event' available as there would be with normal development. You'll need to get the current text from the textbox and then do your logic (you can get the last key by taking a substring if you really need it).

    Check out this example:

    msdn.microsoft.com/.../mt607648.aspx

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

     Aiden

  • Mohsin Ali Profile Picture
    Mohsin Ali 3,614 on at
    RE: Get keycode using onkeypress event in Dynamics CRM

    Hi Aiden, Thank you for your response. Actually I can use addOnKeyPress event. Here is my code snippet that I want to prompt the keycode in dynamics crm. For example, I press 2, it should prompt the key code of 2.

    function checkNumber(fieldName) {

       try {

           debugger;

           var nameKeyPressFunction = function () {

               var key = event.keyCode;

               alert(key);

           }

           Xrm.Page.getControl(fieldName).addOnKeyPress(nameKeyPressFunction);

       } catch (e) {

       }

    }

  • Suggested answer
    Aiden Kaskela Profile Picture
    Aiden Kaskela 19,694 on at
    RE: Get keycode using onkeypress event in Dynamics CRM

    Hi Mohsin,

    You want to use the built in addOnKeyPress methods in CRM - check out the documentation here: msdn.microsoft.com/.../gg334266.aspx

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

     Aiden

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,407 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans