How to set focus on mandatory fields if they are not filled?

GV Saranesh Kum
Kilo Guru

Hi all,

 

I observed, if mandatory fields are not filled and we try to submit the form, it gives the prompt window, stating 'the below are fields are mandatory: fieldname1, fieldname2..". Once we click OK, it sets the focus (cursor) on the unfilled mandatory field. This functionality works only if the field appear on the form, if the field is in section, it doesn't. Any suggestion how to achieve it.

 

Also as mentioned fieldname1, field name2 on the prompt message, they do not follow any order (i am expecting they should be in the order they were placed on the form).

 

kindly help.

 

Regards,

Saranesh.

10 REPLIES 10

marcelo_moreli
Mega Guru

You could merge the original Function:



mandatoryCheck: function () {


      if (!this.checkMandatory || (!this.modified && this.mandatoryOnlyIfModified))


              return true;


      var rc = true;


      var invalidFields = new Array();


      var labels = new Array();


      var missing = this.getMissingFields();


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


              rc = false;


              var field = missing[i];


              var widget = this.getControl(field);


              var tryLabel = false;


              try {


                      widget.focus();


              } catch (e) {


                      tryLabel = true;


              }


              if (tryLabel) {


                      var displayWidget = this.getDisplayBox(field);


                      if (displayWidget) {


                              try {


                                      displayWidget.focus();


                              } catch (exception) {}


                      }


              }


              labels.push(this.tableName + '.' + field);


              var widgetLabel = this.getLabelOf(field);


              var shortLabel = trim(widgetLabel + '');


              invalidFields.push(shortLabel);


      }


      if (!rc) {


              var theText = invalidFields.join(', ');


              theText = getMessage('The following mandatory fields are not filled in') + ': ' + theText;


              try {


                      alert(theText);


                      this.submitted = false;


              } catch (e) {}


      }


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


              this.flash(labels[i], "#FFFACD", 0);


      }


      return rc;


},



With a check to see in which tab the field is, and change the tab (http://www.servicenowguru.com/scripting/client-scripts-scripting/changing-active-tab-selection-servi...).



That is possible to get done, but the effort to get it working is not so small.


BTW, Easier solution is to open a ticket in HI reporting that this is an issue, and let them fix it for you


Hi Marcelo,



This is an expected behaviour as per the design of the platform so not a defect/issue.


If there are mandatory fields within a form section, then an asterisk symbol appears on that form section to indicate that to the user.



Customers can choose to implement client scripts to make the form section flash or focus as suggested by other users here.


Please note that we do not recommend or support DOM manipulation as per the best practices.


Client Script Best Practices - ServiceNow Wiki



Kind regards,


Mansoor Omar | Technical Support Engineer |   User Experience


Hi Mansoor,


This post is from 2 years ago.



Where is it documented that this is as per design?



Thanks.