Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Javascript

Posted on by 1,942

Hi All,

I have a requirement where i need to count the number of characters in a field and display an alert message to the user when it is less than 20.

I am using the following code to execute.

function countchars(){
var fieldtext = Xrm.Page.getAttribute("description").getValue();
if(fieldtext != null){
if(fieldtext.length < 20){
alert(Enter more than 20 characters);
}
}
}

Please advise.

*This post is locked for comments

  • Suggested answer
    SCV Profile Picture
    SCV 1,004 on at
    RE: Javascript

    Hi Vignesh,

    It looks like your entity is an out of the box entity. If not check the field name as it should have your solution prefix. Anyway if you debug your script you should be easily find where the problem is.

    Try looking at following posts to know how to debug javascrips in Dynamics CRM.

    blogs.msdn.microsoft.com/.../debugging-custom-javascript-code-in-crm-using-browser-developer-tools

    crmbook.powerobjects.com/.../debugging-javascript-libraries

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Javascript

    You can use addonkeypress function to get how many characters are there in the field at runtime.

    Refer this:

    community.dynamics.com/.../keypress-methods-in-ms-crm-2016

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Javascript

    Hi Vighnesh,

    The problem in the code is with the alert statement. It should be enclosed in quotes.

    Replace the code as shown below.

    function countchars(){
      var fieldtext = Xrm.Page.getAttribute("description").getValue();
      if(fieldtext != null){
      if(fieldtext.length < 20){
        alert('Enter more than 20 characters');
       }
      }
    }

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Javascript

    OMG
    learn how to debug please. getting immediate answer won't get you far in your career.

  • Suggested answer
    sandeepc Profile Picture
    sandeepc 5,005 on at
  • Suggested answer
    MNarmeen Profile Picture
    MNarmeen 1,846 on at
    RE: Javascript

    I guess the alertDialog works for updated entities only

    Entities were updated in CRM Online 2016 Update 1 and Dynamics 365 Service Pack 1 (on-premises).

    msdn.microsoft.com/.../gg328261.aspx

  • Suggested answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Javascript

    Hi

    Using alert should work (with the correction, previously mentioned) in web. But it is good to use alertDialog instead.

    msdn.microsoft.com/.../jj602956(v=crm.6).aspx

      function countchars() {
                    var fieldtext = Xrm.Page.getAttribute("description").getValue();
                    if ((fieldtext != null) && (fieldtext.length < 20)) {
                        Xrm.Utility.alertDialog("Enter more than 20 characters", null);
                    }
                }


  • Suggested answer
    MNarmeen Profile Picture
    MNarmeen 1,846 on at
    RE: Javascript

    This seems to be alright, except what Gopalan mentioned i.e use "" in alert because it is a custom string not a variable.

    Are you getting any error after this?

  • Suggested answer
    joman Profile Picture
    joman 617 on at
    RE: Javascript

    replace

    alert(Enter more than 20 characters);

    to

    alert("Enter more than 20 characters");
  • Suggested answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Javascript

    Hi

    Code looks ok except

    alert("Enter more than 20 characters");

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans