JavaScript validation

Community Beginner ,
Apr 18, 2022 Apr 18, 2022

Copy link to clipboard

Copied

I have a form with a radio button and I'd like to make some text fields required if that option is checked...

I added the following snippet to my text field but I don't really know ho to test it lol

 

var insuranceField = this.getField("insurance").value;
var policyField = this.getField("policy").value;
if( insuranceField == "insuranceBill" && policyField == ""){
app.alert("Policy Holder Name is required");
}

 

Can anyone help me with this? Tks!!

TOPICS
PDF forms

Views

104

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 ,
Apr 18, 2022 Apr 18, 2022

Copy link to clipboard

Copied

Use this script as a Calculation script in the Text fields:

 

if (this.getField("CHECKBOX1").value != "Off") {event.target.required = true;}
else {event.target.required = false;}

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
Community Beginner ,
Apr 18, 2022 Apr 18, 2022

Copy link to clipboard

Copied

JR tk u for responding.

 

I can't use != "OFF" because they can check a different radion button option so I added

 

if (this.getField("insurance").value != "insuranceBill"){
event.target.required = true;
}else {
event.target.required = false;
}

 But now the policy field has the red border on it but it doesn't go away if I select a different option in the radio button.

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 ,
Apr 18, 2022 Apr 18, 2022

Copy link to clipboard

Copied

If you select other option red circle should go away with that script, but you have problem if neither choices is selected field will be required.

Try changing first line like this:

if (this.getField("insurance").value != "insuranceBill" && this.getField("insurance").value != "Off"){

This should make field required when other choices are selected.

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
Community Beginner ,
Apr 18, 2022 Apr 18, 2022

Copy link to clipboard

Copied

Nesa,

 

I just noticed I checked the option "Required" by accident, that's why the red border didn't go away... so now I just added the initial snippet and there is no red border if I select the radio option I want to make policy field required or even the other options, like nothing is changing.

 

Screen Shot 2022-04-18 at 1.46.07 PM.png

So the first 3 radios are names as "insurance" and the 3rd option has as a value "insuranceBill"

 

policy text field should be required if insuraceBill is checked.

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 ,
Apr 18, 2022 Apr 18, 2022

Copy link to clipboard

Copied

If you want it required when "insuranceBill" is checked then change  != "insuranceBill" to == "insuranceBill".

This should do the trick:

event.target.required = this.getField("insurance").value == "insuranceBill" ? true : false;

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
Community Beginner ,
Apr 18, 2022 Apr 18, 2022

Copy link to clipboard

Copied

I did... not working . I'm attaching the document here...

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 ,
Apr 18, 2022 Apr 18, 2022

Copy link to clipboard

Copied

Move script to calculate tab.

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
Community Beginner ,
Apr 18, 2022 Apr 18, 2022

Copy link to clipboard

Copied

LATEST

omg tk u so much!!! It is working!

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