Mandatory field validation infinite loop query...

Explorer ,
May 13, 2019

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript, Macintosh

Views

156

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Mandatory field validation infinite loop query...

Explorer ,
May 13, 2019

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript, Macintosh

Views

157

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 13, 2019 0
Most Valuable Participant ,
May 13, 2019

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 13, 2019 0
Explorer ,
May 13, 2019

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 13, 2019 0
Most Valuable Participant ,
May 13, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 13, 2019 0
Explorer ,
May 13, 2019

Copy link to clipboard

Copied

Ok, thanks for the help.

I can test for the user entering spaces ok but when trying to test for null that doesn't work.

The user can tab straight out of the form field without the validation working.

Clearly I'm not testing for the correct value.

Please can you point me in the right direction?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 13, 2019 0
Most Valuable Participant ,
May 13, 2019

Copy link to clipboard

Copied

I don't think you should do that. It's very user-unfriendly. Some people use the tab to switch between fields and to cause them to get "stuck" in a field until they fill it in is not good practice.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 13, 2019 0
Explorer ,
May 13, 2019

Copy link to clipboard

Copied

Thanks for your reply.

Is is possible to test for that action though because the field that requires filling in?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 13, 2019 0
Most Valuable Participant ,
May 13, 2019

Copy link to clipboard

Copied

Sorry, I don't follow. That is the On Blur event.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 13, 2019 0
Explorer ,
May 13, 2019

Copy link to clipboard

Copied

If the user clicks in the first text box field, and without typing anything, tabs to the second text box field what value does the first box have?

I've tried validating for a null value but that doesn't do it.

By tabbing out of the first text field box to the second, the validation seems to be missed?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 13, 2019 0
Most Valuable Participant ,
May 13, 2019

Copy link to clipboard

Copied

The value is probably not null but an empty string.

And yes, if you don't change the field's value, the validation event doesn't get triggered.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 13, 2019 0
Explorer ,
May 13, 2019

Copy link to clipboard

Copied

Thanks again for the help.

Hhhmmm, so I can't use the validation to check for such an occurence.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 13, 2019 0
Most Valuable Participant ,
May 13, 2019

Copy link to clipboard

Copied

No.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 13, 2019 0
Explorer ,
May 14, 2019

Copy link to clipboard

Copied

Joel and try67, thank you for your help with this one.

Joel, will have to read up on that approach.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 14, 2019 0
Adobe Community Professional ,
May 13, 2019

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 13, 2019 0