Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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();
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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).
Copy link to clipboard
Copied
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();
}
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more