Skip to main content
deckarduk
Inspiring
May 13, 2019
Question

Mandatory field validation infinite loop query...

  • May 13, 2019
  • 2 replies
  • 1453 views

Hi everybody,

Please can someone help me out.

I've a number of 'mandatory' text fields on a form.

I've setup a 'Document Javascript' that sets the focus to the first mandatory field upon opening the pdf.

Using an onBlur action I'm testing to see if the user has entered something in the first text field.

If they haven't, e.g. they've just tabbed to the next field, they get a popup asking them to fill in the field and then the focus is set back on that field.

If the user only enters a space, and then tabs onto the next field, the text field value is checked to see if that's the case. If it is, the user gets a popup and then the focus is set back to the text field.

If the user enters something other than just spaces they are ok.

The problem I've got is that the form gets stuck in a loop between the action of tabbing to the next field and the onBlur test.

Here's my code.

var fld = this.getField("TextBox1");

var nxtFld = this.getField("TextBox2");

 

if (fld.value !== "") {  //if field is not empty 

var emptyTest = /^\s*$/;

if(emptyTest.test(fld.value)) {

    app.alert("Only spaces, please enter the relevant information");

    fld.value = "";

    fld.setFocus();

}

else {  //if field is empty

 

    app.alert("This field is required.\n\nPlease fill in the Name field2");

   fld.setFocus();

}

Hope that makes sense.

Please can someone help.

Thanks in advance.

This topic has been closed for replies.

2 replies

Joel Geraci
Community Expert
Community Expert
May 13, 2019

A better method of accomplishing this is to use the app.response method when the document first opens to capture the input for the first field and keep showing it until an appropriate value is entered. Then use that value to populate the first field.

try67
Community Expert
Community Expert
May 13, 2019

I would recommend not using the setFocus command, but if you have to do it then use the Validation event, not On Blur.

If you do that you could also set event.rc to false in order to reject the incorrect values.

deckarduk
deckardukAuthor
Inspiring
May 13, 2019

Hi try67,

Thanks for the help.

I did try a validation script but had problems with setting the focus back onto the incorrect field?

Isn't the validation script triggered when the user moves from one field to the next?

try67
Community Expert
Community Expert
May 13, 2019

No, only when they change a field's value and then exit it.