Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Javascript If Statement in text field not working when Value is a number

New Here ,
Aug 24, 2021 Aug 24, 2021

Copy link to clipboard

Copied

I creating a form in Adobe Acrobat Pro DC. I have a text field called "Job Number" & a dropdown field called "DEPT". Each Dept has their own set of Job#s and they can start with either a letter or a number depending on which Dept it is for. I wrote Javascript & set it so that on blur for the Dept dropdown it Verifies that the Job Number starts with the correct letter or number for that Dept. and if it doesnt match an alert window pops up telling you its wrong. For testing purposes I added an alert to the else statement. It works as expected for Job Numbers that start with a letter but if it is a number it doesn't work. What am I missing or doing wrong? I am new to Javascript so I apologize if this is something super simple! Thanks for any help you can give me.

Here is what I have:

var v = this.getField("DEPT").value;
var job1 = this.getField("Job Number").value;
var job2 = job1.toLowerCase();
var job3 = this.getField("Job Number").valueAsString;

if(job2.charAt(0) != "m" && job2.charAt(0) != "r" && job2.charAt(0) != "s" && v == "Dept 2") {
    app.alert("Dept 2 Job Number must start with M, R, or S");
    this.getField("Job Number.0").setFocus();	
}
else if(job2.charAt(0) != "k" && v == "Dept 3") {
    app.alert("Dept 3 Job Number must start with a K");
    this.getField("Job Number.0").setFocus();
}
else if(job2.charAt(0) != "i" && v == "Dept 4") {
    app.alert("Dept 4 Job Number must start with a I");
    this.getField("Job Number.0").setFocus();
}
else if(job2.charAt(0) != "t" && v == "Dept 5") {
    app.alert("Dept 5 Job Number must start with a T");
    this.getField("Job Number.0").setFocus();
}
else if(job2.charAt(0) != "h" && v == "Dept 6") {
    app.alert("Dept 6 Job Number must start with H");
    this.getField("Job Number.0").setFocus();
}
else if(job2.charAt(0) != "a" && job2.charAt(0) != "l" && job3.charAt(0) != "0" && v == "Dept 7") {
    app.alert("Dept 7 Job Number must start with A, L, or 09");
    this.getField("Job Number.0").setFocus();
}
else if(job2.charAt(0) != "f" && job2.charAt(0) != "w" && v == "Dept 8") {
    app.alert("Dept 8 Job Number must start with F or W");
    this.getField("Job Number.0").setFocus();
}
else if(job2.charAt(0) != "c" && v == "Dept 22") {
    app.alert("Dept 22 Job Number must start with CR or CF");
    this.getField("Job Number.0").setFocus();
}
else if(job2.charAt(0) != "b" && job3.charAt(0) != "0" && v == "Dept 26") {
    app.alert("Dept 26 Job Number must start with B or 03");
    this.getField("Job Number.0").setFocus();
}
else if(job2.charAt(0) != "e" && v == "Dept 50") {
    app.alert("Dept 50 Job Number must start with a E");
    this.getField("Job Number.0").setFocus();
}
else if(job2.charAt(0) != [0-9] && v == "Dept 80") {
    app.alert("Dept 80 Job Number must start with a number");
    this.getField("Job Number.0").setFocus();
}
else if(job2.charAt(0) != "y" && v == "Dept 81") {
    app.alert("Dept 81 Job Number must start with a Y");
    this.getField("Job Number.0").setFocus();
}
else if(job2.charAt(0) != "q" && v == "Dept 96") {
    app.alert("Dept 96 Job Number must start with a Q");
    this.getField("Job Number.0").setFocus();
}
else{app.alert("DEPT & Job# are entered correctly");
}

 

TOPICS
JavaScript, PDF forms

Views

32

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
community guidelines
Adobe Community Professional ,
Aug 24, 2021 Aug 24, 2021

Copy link to clipboard

Copied

Following is not possible:

 job2.charAt(0) != [0-9]

Check the javascript console for errors.

 

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
community guidelines
New Here ,
Aug 24, 2021 Aug 24, 2021

Copy link to clipboard

Copied

That didn't work. Is this easier to follow?

var v = this.getField("DEPT").value;
var job1 = this.getField("Job Number").valueAsString;
var job2 = job1.toLowerCase();

if(job2.charAt(0) != "m" && v == "Dept 2") {
    app.alert("Dept 2 Job# must start with M");	
}
else if(job2.charAt(0) != "l" && job1.charAt(0) != [0-9] && v == "Dept 7") {
    app.alert("Dept 7 Job# must start with L or 09");
}
else if(job2.charAt(0) != [0-9] && v == "Dept 80") {
    app.alert("Dept 80 Job# must start with a number");
}
else{
	app.alert("DEPT & Job# are entered correctly");
}

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
community guidelines
Adobe Community Professional ,
Aug 25, 2021 Aug 25, 2021

Copy link to clipboard

Copied

LATEST

You didn't fix the problem. Are you trying to test whether the string begins with any digit, or whether it begins with the string "09", as you error message suggests?

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
community guidelines