web
You’re offline. This is a read only version of the page.
close
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,634

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

I have the same question (0)
  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    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

  • Mohsin Ali Profile Picture
    3,634 on at

    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
    19,696 on at

    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

  • Verified answer
    Community Member Profile Picture
    on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans