Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

syntaxError: function statement requires a name error.

Community Beginner ,
Jul 16, 2025 Jul 16, 2025

I have this document JavaScript that is suppose to work in conjunction with a fields custom validation script, which is to prevent a user from leaving a field blank. It does not seem to work. In the Console I get:

SyntaxError: function statement requires a name
1:Doc:Exec

 

Any ideas would be much appriciated.

 

Document JavaScript

function isFieldBlank(fieldName) {
console.println("Running isFieldBlank on: " + fieldName);
var field = this.getField(fieldName);

if (!field) {
console.println("Field not found: " + fieldName);
return true;
}

var val = field.value;
if (typeof val === "string" && val.trim() === "") {
console.println(fieldName + " is blank.");
app.alert("This field cannot be left blank.");
field.setFocus();
return true;
}

console.println(fieldName + " has value: " + val);
return false;
}



Custom Validation Script

event.rc = !isFieldBlank(event.target.name);

 

TOPICS
JavaScript
617
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Jul 16, 2025 Jul 16, 2025

My script will work, with a slight modification, if the field is formatted as a number:

if(event.value<0)
{
app.alert("Field must be zero or greater.",1);
event.target.setFocus();
}

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 16, 2025 Jul 16, 2025

So there are a couple of problems with this script, neither of which is a syntax error.   Your syntax error is somewhere else. 

 

The main error is this line:

var field = this.getField(fieldName);

 

The field object is already available in "event.target".  But this doesn't help you either because the value of the field is not the value entered by the user, but the previous value. The validate script runs before the new entered value is committed to the field value. 

To get the newly entered value use "event.value". 

 

So, there is no need to pass the field name into the scirpt. 

 

But the second problem is that your script seems to be intended for a form level validation. You see, the validation script is only run when the user deliberately enters data into the field. So if they leave the field blank, the script will never be run. 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 16, 2025 Jul 16, 2025

Adding to @Thom Parker 's post, "which is to prevent a user from leaving a field blank".  What is the trigger for leaving a blank field?  What are you trying to accomplish?  This On Blur action script will prevent the user leaving a blank field, but I wouldn't recommend it as the user will get stuck in that field until a value is entered:

if(!event.value)
{
app.alert("Field cannot be left blank.");
event.target.setFocus();
}

Is your script for a submit button?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 16, 2025 Jul 16, 2025

Thank you to both of you for your replies.

 

What I would like to accomplis is actuall 2 things if the user exits out of the field I would like an App Alert "The fied mus be 0 or greater. Then return the focus to the field (RerturnedTickets).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 16, 2025 Jul 16, 2025

My script will work, with a slight modification, if the field is formatted as a number:

if(event.value<0)
{
app.alert("Field must be zero or greater.",1);
event.target.setFocus();
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 16, 2025 Jul 16, 2025

Thak you so much.

I changed <=0 becaus I want to allow 0.

Hope I am not being pushy I have a lot of fields I would like to apply this to. Can this be converted to a Document JavaScript?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 16, 2025 Jul 16, 2025
LATEST

You can turn it into a function in a document level script.  However, you would still have to paste the function into all the fields you want to apply this to.  Since the script does not name the field, there is no time saving whether you paste the script or the function that calls the doc level script.

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