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)

javascript example for minimum value with a set focus and prevent save

(0) ShareShare
ReportReport
Posted on by

Hi, can someone share an example to:

  1. set a minimum value for a field
  2. with a set focus back on the field if they try to save
  3. and an on save event that will prevent the form from being saved

I have the field set to allow for 12 characters and I need the value to validate that there are 12 characters.

Dynamics 2016 on-prem.

Thank you in advance!

*This post is locked for comments

I have the same question (0)
  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi kmc333,

    Here you go. Add the below method on change of your field. Do remember to update the schma name of your field. The below script I used for the account name field.

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

    function checkNameLength()

    {

       var currentName = Xrm.Page.getAttribute("name").getValue();

       if(currentName.length <20)

       {

           Xrm.Page.getControl("name").setNotification("Field length should be 20 characters");

       }

       else

       {

           Xrm.Page.getControl("name").clearNotification();

       }

    }

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

    I hope this helps. If my post answers your questions, please mark it helpful/ verified.

  • Community Member Profile Picture
    on at

    Hi Ravi, I have something similar in place which works to set the alert to be sure to have the correct length to the user but I can't get the focus to set back on the field and also prevent the save from occurring. Essentially the user can still save the form without setting the field properly with the amount of characters I need.

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    The above script doesn't give you an alert instead it will set the field notification with the * mark and then you won't be able to save the recor dunless that field error is fixed.

    Try the above script and let me know how it goes.

    neilparkhurst.com/.../javascript-notifications

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Hi,

    Just to add what has not yet been answered:

    Xrm.Page.getControl(“controlname”).setFocus();

    Take a look at the following blog post:

    community.dynamics.com/.../crm-2015-how-to-set-focus-in-javascript

    For preventing to save a form, see the following post:

    neilparkhurst.com/.../javascript-prevent-save

    Hope this helps.

  • Community Member Profile Picture
    on at

    Ravi, Many Thanks!!! This worked perfectly!

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Glad it worked :)

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