Expand my Community achievements bar.

Help with functions required

Avatar

Level 2

We have a requirement to include a 'Check form' button on all our forms. This goes through the form identifying whether mandatory text fields have a value or not and then populates the text value of a hidden/visible error message box.

This all works fine but writing 200+ lines of codes for the same sort of task is clearly not an efficient way to do things.

For this reason I have tried to create a function to do the same thing. What I want it to do is check whether a field has a value or not and if not populate an error text field with a list of fields that failed the validation.

I can do this up to a point but I want the error message to contain the name of the field without having to define it.

I've been struggling with a way to populate the text string with the name of the field. I tried looking at the path in XML to try and extract the caption value but the best I get is "-[objectXFAObject] is blank" or undefined is blank.

This what I have so far:

------

var nameField = oField.field.caption.text.value

var errorVar = ""

function validEntry(oField){

if ((oField.rawValue == null) || (oField.rawValue == ""))

{

errorVar = "Please note" + "\n \t -" + nameField + " is blank";

form1.jsTest.errorMessag.TextField2.rawValue = errorVar;

}

else if ((oField.rawValue == null) || (oField.rawValue == ""))

{

form1.jsTest.errorMessag.TextField2.rawValue = "No Mistake";

errorVar = "";

}

}

------

The text fields have

blankField.validEntry(this)

in the exit events.
2 Replies

Avatar

Former Community Member

The attached form contains three object types - text field, radio button exclusion group and a check box. I added a button that performs global form field validation for nulls (text fields), 0 (check boxes) and empty (exclusion groups), excluding the validate button, the reset form button and the mandatory text field object. The image below depicts the result of checking "yes" in the radio button exclusion group and clicking "Validate". When errors occur "Mandatory Fields" is made visible and populated with an error message.

Untitled.png

This is a technique I have used to iterate over a subform on the click event of the "Validate" button and get the fields by class name.

// form1.subform1.validateBtn::click - (JavaScript, client)


form1.subform1.mandatoryFields.rawValue = "";

var errorStr = "";

var oNodes = subform1.nodes;


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

  if (oNodes.item(i).className == "field" || oNodes.item(i).className == "exclGroup") {

    // text fields and check boxes are of class "fields"

    if (oNodes.item(i).className == "field") {

      // tricky checking for null check boxes because the default value is "0" where "0" in a text field may be valid

      if (oNodes.item(i).rawValue == null || oNodes.item(i).rawValue == "0") {

        // exclude the validation button, the reset button and the mandatory fields text field from the test

        if (!(oNodes.item(i).name == "validateBtn" || oNodes.item(i).name == "mandatoryFields" || oNodes.item(i).name == "resetBtn")) {

          errorStr = errorStr + oNodes.item(i).name + " is a mandatory field" + "\n";

        }

      }

    }

    else {

      // radio button exclusion groups are of class "exclGroup"

      if (oNodes.item(i).rawValue == "") {

        errorStr = errorStr + oNodes.item(i).name + " is a mandatory field" + "\n";

      }

    }

  }

}


if (errorStr != "") {

  form1.subform1.mandatoryFields.rawValue = errorStr;

  form1.subform1.mandatoryFields.presence = "visible";

}

else {

  form1.subform1.mandatoryFields.presence = "hidden";

}

Steve

Avatar

Level 2

Steve,

Thank you so very much.  This is exactly what I have been looking for!  I will give it a try and see how I get on.

Thanks again,

Rich