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)

Checking if all the fields have Value before Submitting a form using JavaScript

(0) ShareShare
ReportReport
Posted on by 274

Hello,

I created a button using Ribbon Workbench, I have a Submit and Reject button. When a user use "Submit" button, I would like to check if all the fields have value, the fields in the form are business recommended however when it's ready to be submitted I want all the fields to have value. 

Thanks!

*This post is locked for comments

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    I believe this can be done using JavaScript. Something like following:

    var allfieldspopulated  = true;

    var fields = ["field1", "field2", "field3"];

    for(var i = 0; i < fields.length; i++){

    if (Xrm.Page.getAttribute(fields[i]).getValue() == null){

    allfieldspopulated = false;

    break;

    }

    }

    if (!allfieldspopulated){

    //put your logic if fields are not populated here.

    }

  • MituCRMing Profile Picture
    274 on at

    Great, thanks!!

  • MituCRMing Profile Picture
    274 on at

    Hi Andrii,

    The above code works, however I added alert in if(!allfieldsPopulated) as follow..even if field 1 has a value It still show the alert on field 1. Also how can I pass the name of the field in the alert message? Thanks

    var allfieldspopulated  = true;

    var fields = ["field1", "field2", "field3"];

    for(var i = 0; i < fields.length; i++){

    if (Xrm.Page.getAttribute(fields[i]).getValue() == null){

    allfieldspopulated = false;

    break;

    }

    }

    if (!allfieldspopulated){

     alert("Error: " + "field1");

      alert("Error: " + "field2");

    }

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    In your initial message you wrote to validate all fields. So if one field has have and second doesn't - message will come up anyway.

    The name of field you can get using following code:

    Xrm.Page.getControl("fieldname").getLabel()

  • MituCRMing Profile Picture
    274 on at

    Oh I see, but I want the message to come for the field that doesn't have a value not for those have value.

    I appreciate your help!

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Ok. In that case you can use something like following:

    var fields = ["field1", "field2", "field3"];

    var message = "";

    for(var i = 0; i < fields.length; i++){

    if (Xrm.Page.getAttribute(fields[i]).getValue() == null){

    message += (message === "" ? "" : "\r\n") + "Field " + Xrm.Page.getControl(fields[i]).getLabel() + " doesn't contain value";

    }

    }

    if (message != ""){

    alert(message);

    }

  • MituCRMing Profile Picture
    274 on at

    Thanks. I am trying to make the fields that doesn't have a value to be required. I added this line of code

    Xrm.Page.getAttribute(fields[i]).setRequiredLevel("required");

    However it only require the first field that doesn't have the value not all the fields that doesn't have a value.

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Why don't you want to make a field required in attribute options? Just make it required and you will not need any coding.

  • Maksym Tomyn Profile Picture
    30 on at

    Hello,

    you can use javascript validation

    my example finds first field if field is recommended, visible and not disabled

    and parent section and parent tab are visible

    var i = 0, j = 0,

       allFields = Xrm.Page.getAttribute(),

       fieldControls,

       isValid = true;

    do {

       if (allFields[i].getRequiredLevel() == "recommended" && allFields[i].getUserPrivilege().canUpdate && allFields[i].getValue() == null){

           fieldControls = allFields[i].controls.get();

           do {

               if (fieldControls[j].getVisible() && !fieldControls[j].getDisabled() && fieldControls[j].getParent().getVisible() && fieldControls[j].getParent().getParent().getVisible()){

                   isValid = false;

                   //----your code----//

                   fieldControls[j].setFocus(); //msdn.microsoft.com/.../gg334266.aspx;MSPPError=-2147217396#BKMK_setFocus

               }

               j++;

           } while (isValid && j < fieldControls.length)

       }

       i++;

    } while (isValid && i < allFields.length)

  • MituCRMing Profile Picture
    274 on at

    Hi Andrii,

    I didn't want the fields to be required by default that's why.

    Thanks

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