Jump to content

Complex if Statement for Biz-Card


panthermac

Recommended Posts

Good morning,

 

Have a request from a customer that involves waaaay more javascript programming than I've ever done (and my head hurts pretty bad so far...) I'm new to the javascript end of Printables...

 

Customer has Address information on the bottom of the card, and use a bullet to separate Address lines 1 and 2, from City, state Zip.

 

Addr1, Add2 • City, State Zip

 

I've handled the comma's w/ the pre-formatted rule handily. However some folks will use a state only, or even a city only, no state or zip.. and handling the bullet point isn't working well for me at all (again, I'm new to Javascript, and smoked a lot of dope in college, so.... :rolleyes: )

 

I'm pretty sure what I want/need to do is write a complex if statement, involving all of variable's (City, State, Zip), that appear to the Right of the bullet? If all of those fields are blank, I don't want a bullet. If any one of them contains info, I need a bullet to separate Addr1 and Addr2 from the other fields. It is also possible I could have no info for addy's 1+2, and be given just a state field to populate (so there's several permutations on how my data may come in, and I'm not able to properly handle the bullet's presence w/ Javascript).

 

Here's what I came up w/ on my own, but I keep getting syntax errors on the 'else' statement (!?) and it won't work for me:

if (Field("City") == "") + (Field("State") == "") + (Field("Zip") == "")
return "";

if (Field("City") != "") + (Field("State") == "") + (Field("Zip") == "")
   return ' • '+Field("City");

if (Field("City") == "") + (Field("State") != "") + (Field("Zip") == "")
   return ' • '+Field("State");

if (Field("City") == "") + (Field("State") == "") + (Field("Zip") != "")
   return ' • '+Field("Zip");

else 
   return ' • '+Field("City")', '+Field("State")' '+Field("Zip");

 

Thanks for the help!

 

- Mac

Link to comment
Share on other sites

Funny, I had friends who seemed MORE intelligent after smoking weed, but then I haven't seen them lately to see if the initial results have backfired...

 

Instead of using a plus (+) in your IF statements, you should be using a double ampersand (&&). I'm guessing you are getting an error on the ELSE because FP doesn't know where the IF ends and the ELSE begins.

 

I'm not the expert around here, but if I understand what you are trying to do, I would write something like this:

//determine street address
var MyAddress = Field("Address1") + ", " + Field("Address2");
if (Left(MyAddress,1) == ",") { //remove ", " for records with Address2 only
  MyAddress == Right(MyAddress,MyAddress.length - 2);
  }

//determine city/state/zip
var MyCSZ = Field("City") + ", " + Field("State") + " " + Field("Zip");
for (i=1;i<=2;i++) { //remove punctuation from records with empty fields
  if ((Left(MyCSZ,1) == ",") || (Left(MyCSZ,1) == " ")){
     MyCSZ == Right(MyCSZ,MyCSZ.length - 1);
     }
  }

//put it all together
var FinalAddress = MyAddress + " • " + MyCSZ;
if (Left(FinalAddress,1) == " ") { //remove " • " for records with C, S and/or Z only
  FinalAddress == Right(FinalAddress,FinalAddress.length - 3);
  }

return FinalAddress;

Link to comment
Share on other sites

Funny, I had friends who seemed MORE intelligent after smoking weed, but then I haven't seen them lately to see if the initial results have backfired...

 

LOL! Maybe they seemed more intelligent, I've had that side effect! :D

 

Thanks for the help, but I could barely even understand the code supplied. Don't know how to call out variables, and that good stuff. Simple if/then statements stump me, as you can see, so... I'll take that Ampersand thing you said and try to run with it! LOL!

 

Thanks!!

 

- Mac

Link to comment
Share on other sites

While I don't mind explaining anything I wrote (that's how I learned via this forum as well), I can say that if you just skim through the code and correct any fields that don't match yours, the code should work as is. In theory, you don't need to understand it to use it. :)

 

The only field names that potentially need editing on your end would be Address1, Address2, City, State, and Zip.

Link to comment
Share on other sites

In theory, you don't need to understand it to use it. :)

 

Boy, there's a couple good jokes just waiting right there.. but I fear this one's already far enough off topic! LOL!

 

Thanks for the tip Eric, I'll take a crack at it this afternoon (after all, I didn't understand the other javascripts when I first started using them!)

Thanks again.

 

- Mac

Link to comment
Share on other sites

Thanks Eric, that seemed to sorta work!

I'm getting some extra comma's though, on a few of the test data sets I've been using. I've got a flatfile w/ various permutations in it, that I've used for this customer's biz-cards before. I've usually gotta tweak the rules around a little to satisfactorily capture all the combinations, but w/ the new code I'm in unfamiliar territory. Can you point out which section I should play w/ to try and get the extra comma's removed (when one of the fields is left blank).

 

For example, I get a comma after Addr1, when Addr2 is blank, so the output reads:

 

Addr1, • City, ST 12345

If just state is entered, I get:

 

, • , NY

And little things like that. I want to get rid of those comma's that appear after/before empty fields. I've got a rule that I'm used to using (one of the supplied ones :rolleyes: ), to remove the comma that comes before a field, but I've never had to string them all together like this before (and remove a bullet as well, depending on the combination of data fields...). It's the compounding of possible variables that's throwing my old code out the window on that method, I don't know if it's capable of doing what I want (I think it might, I just have to sit down and try to resuscitate a few brain cells).

 

Thanks again for the help!

 

- Mac

Link to comment
Share on other sites

//determine street address
var MyAddress = Field("Address1") + ", " + Field("Address2");
if (Left(MyAddress,1) == ",") { //remove ", " for records with Add2 only
  MyAddress == Right(MyAddress,MyAddress.length - 2);
  }
[color="Red"]if (Right(MyAddress,2) == ", " { //remove ", " for records w/ Add1 only
  MyAddress == Left(MyAddress,MyAddress.length - 2);
  }[/color]

//determine city/state/zip
var MyCSZ = Field("City") + ", " + Field("State") + " " + Field("Zip");
for (i=1;i<=2;i++) { //remove punctuation from records with empty fields
  if ((Left(MyCSZ,1) == ",") || (Left(MyCSZ,1) == " ")){
     MyCSZ == Right(MyCSZ,MyCSZ.length - 1);
     }
  }

//put it all together
var FinalAddress = MyAddress + " • " + MyCSZ;
if (Left(FinalAddress,1) == " ") { //remove " • " for records with C, S and/or Z only
  FinalAddress == Right(FinalAddress,FinalAddress.length - 3);
  }
[color="blue"]if (Right(FinalAddress,2) == "• ") { //records with street address(es) only 
  FinalAddress = Left(FinalAddress,FinalAddress.length - 3);
  }[/color]

return FinalAddress;

I added the code in red above to deal with the "Address1, " situation.

 

Also, I noticed you mentioned the possibility of records with no bullet needed at all which my original code does not (totally) account for. I added the code in blue to deal with those scenarios.

 

As for records with state only preceded by a comma, I'm not sure how that is happening since the code in the middle section is checking the first two characters of the City/State/Zip variable for commas and spaces and removing them. Are we sure the fields themselves don't have extraneous characters?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...