Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Validating fields

(0) ShareShare
ReportReport
Posted on by 80

Hello,

I have a single line of text field called 'A'. In this field I want to insert/update only numbers. What would be the best way for this? If you have the code can you please share? 

Current CRM version : 2015

*This post is locked for comments

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: Validating fields

    Ah! it should not be onsave , s attach onchange of accountnumber  field.

  • Vishnu Kumar9 Profile Picture
    Vishnu Kumar9 80 on at
    RE: Validating fields

    As mentioned, my field name is test_accountnumber. Below is the code I kept in form for onsave. I dont see any issues with the field name

     function OnChangeAccountNumber() {  

         var accountNumber = Xrm.Page.getAttribute("test_accountnumber").getValue();

         if (accountNumber !== null && accountNumber !== "") {

             if (isNaN(accountNumber)) {

                 Xrm.Page.getAttribute("test_accountnumber").setValue(null);

                 Xrm.Page.getControl("accountnumber").setNotification("Please enter only numbers", "CHKNUM");

             }

             else {

                 Xrm.Page.getControl("test_accountnumber").clearNotification("CHKNUM");

             }

         }

         else {

             Xrm.Page.getControl("test_accountnumber").clearNotification("CHKNUM");

         }

     }

  • gdas Profile Picture
    gdas 50,089 on at
    RE: Validating fields

    Please check field name

  • Vishnu Kumar9 Profile Picture
    Vishnu Kumar9 80 on at
    RE: Validating fields

    Now it is throwing error as unable to get property set notification of undefined or null reference

  • Verified answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: Validating fields

    Try with this , Please make sure the field name is correct highlighted below I did the changes .

      function OnChangeAccountNumber() {  
          var accountNumber = Xrm.Page.getAttribute("accountnumber").getValue();
          if (accountNumber !== null && accountNumber !== "") {
              if (isNaN(accountNumber)) {
                  Xrm.Page.getAttribute("accountnumber").setValue(null);
                  Xrm.Page.getControl("accountnumber").setNotification("Please enter only numbers", "CHKNUM");
               
              }
              else {
                  Xrm.Page.getControl("accountnumber").clearNotification("CHKNUM");
              }
          }
          else { 
              Xrm.Page.getControl("accountnumber").clearNotification("CHKNUM");
          }
      }


  • Vishnu Kumar9 Profile Picture
    Vishnu Kumar9 80 on at
    RE: Validating fields

    My field name is test_accountnumber. When I place this code, it is throwing error as unable to get property clear notification of undefined or null reference. Can you provide the code?

    function OnChangeAccountNumber() {    

      var accountNumber = Xrm.Page.getAttribute("test_accountnumber").getValue();

      if (accountNumber !== "") {

          if (isNaN(accountNumber)) {

              Xrm.Page.getControl("accountnumber").setNotification("Please enter only numbers", "CHKNUM");

          }

          else {

              Xrm.Page.getControl("accountnumber").clearNotification("CHKNUM");

          }

      }

      else {        

          Xrm.Page.getControl("accountnumber").clearNotification("CHKNUM");

      }

    }

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: Validating fields

    This is alternate solution for onkkeypress in CRM 2015. Its depends on your requirement whether you go for onchange or onkeypress.

    If your customer agreed with onchange behavior go for Ravi's suggestion .

  • Vishnu Kumar9 Profile Picture
    Vishnu Kumar9 80 on at
    RE: Validating fields

    Thanks Goutam. In your post, you mentioned it as unsupported way.  Can we still use it?

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: Validating fields

    Both code will work . In have already shared you can try   my second  suggestion which will work for keypress event which means you cant type other characters except numeric. Just put the code in the webresource you dont need to attach the forms anywhere just replace the field name,

    Ravi's suggestion will also work , it will fire onchange event , so attach the function in onchange event.

  • Vishnu Kumar9 Profile Picture
    Vishnu Kumar9 80 on at
    RE: Validating fields

    Goutam,

    which code will work effectively to check the number for single of text field. Is it your's or Ravi's?

    If it is your code, can you provide the code for it please?  Somehow When I use your code it is throwing error for addonkeypress? If you can provide code for the field 'A' , that would be great

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

Announcing Our 2025 Season 1 Super Users!

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

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

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,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans