These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Field Validation

MFARRELL
Registered: Aug 17 2010
Posts: 17
Answered

Good afternoon,

I am new to Javascript and have been living off this forum along with google for most of my script needs. It has been working out until I hit this snag.

I am trying to compare two fields. If they are the same send an alert saying they are and also clear them. Using this code.

if (getField("REF ADD").value == getField("REF2 ADD").value);
{
app.alert("The References cannot have the same address.\n\nPlease change the addresses.")
(getField("REF ADD").value = "")
(getField("REF2 ADD").value = "")
}

This action was placed the the custom validation area on the "REF2 ADD" field. It shows the Alert and clears the first, "REF ADD", field but not the second. Also it doesnt matter what is in the fields it always comes back true.

These two field are address fields, both alpha and numeric.

Any help is appreciated.

Thank you.

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Remove the semi-colon after the if condition.
Don't put the getField commands in parentheses.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

MFARRELL
Registered: Aug 17 2010
Posts: 17
if (getField("REF ADD").value == getField("REF2 ADD").value)
{
app.alert("The References cannot have the same address.\n\nPlease change the addresses.")
getField("REF ADD").value = ""
getField("REF2 ADD").value = ""

}


Isnt working for me. Maybe there is another way around this. Maybe im approaching this wrong.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Try adding this. before getField, so this.getField("...")

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

MFARRELL
Registered: Aug 17 2010
Posts: 17
No avail. I have been switching stuff around for 2 days trying to figure this out and I cant seem to make it work...
try67
Expert
Registered: Oct 30 2008
Posts: 2398
The problem lays in the order that you're resetting the fields. This works better:
if (this.getField("REF ADD").value == event.value) {app.alert("The References cannot have the same address.\n\nPlease change the addresses.")event.value = "";this.getField("REF ADD").value = "";}

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
When you have to access a field's value while you are focused on it, you should use the 'event' object.

Remove the semicolon at the end of the 'if' statement, that punctuation marks the end of the statement to be executed for the 'if' statement.

Remove the parentheses from the code clearing the fields.

So for the "REF2 ADD" field:

if (this.getField("REF ADD").value == event.value){app.alert("The References cannot have the same address.\n\nPlease change the addresses.");this.getField("REF ADD").value = "";event.value = "";}

The reason for this is that the field is not updated until the field loses focus.

George Kaiser

MFARRELL
Registered: Aug 17 2010
Posts: 17
Thanks guys I appreciate all the help and the timely responses.
MFARRELL
Registered: Aug 17 2010
Posts: 17
Ok I ran into a problem. I also have a button that clears the form upon submisson. It is returning the same alert box because the 2 empty values are the same. Its not hurting anything but look bad upon submisson.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You could add code to only run the code if either or both fields are not empty.
if ( (this.getField("REF ADD").value == event.value) & ( this.getField("REF ADD").value != "" |  event.value != "")  ){app.alert("The References cannot have the same address.\n\nPlease change the addresses.");this.getField("REF ADD").value = "";event.value = "";}

George Kaiser