Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

preventing event from happening on attached onkeypress function

Posted on by 70
function AttachKeyPressValidation(){
    Xrm.Page.getControl("new_field").addOnKeyPress(ValidateOnlyNumber);
}

var ValidateOnlyNumber = function (context) {
    var field_value = context.getEventSource().getValue();   
    if (isNaN(field_value )) {
         /// EVENT MUST RETURN FALSE HERE TO PREVENT INPUT
    }
} 
How can I reach to the event from the context here to make it return false to prevent invalid input on key press event.


*This post is locked for comments

  • okank Profile Picture
    okank 70 on at
    RE: preventing event from happening on attached onkeypress function

    I couldn't find a way to access the key press event and return false so I did it like you, setting the attribute.

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: preventing event from happening on attached onkeypress function

    Hi okank,

    I am not sure if you would be able to get the context abd return false but you can achieve this requirement by following script. I have added this on account number field of account entity-

    ============

    function AttachKeyPressValidation() {

       var keyPressFcn = function () {        

           var userInput = Xrm.Page.getControl("accountnumber").getValue();

           if (isNaN(userInput)) {

               var f = userInput.substring(0, userInput.length);

               Xrm.Page.getAttribute("accountnumber").setValue(f);

           }        

       };

       Xrm.Page.getControl("accountnumber").addOnKeyPress(keyPressFcn);

    }

    ================

    Hope this helps.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans