Jump to content

Code Question


ksulliv1

Recommended Posts

Does anyone see an obvious problem with this code? I am working on a web form that can have up to three phone numbers or just one. Each time a number is entered I need to add the appropriate initial before the number entered in a different font and size. So I have created three rules, one for each of the three phone number fields that is based off a drop down list where the user picks "Office" "Mobile" "Fax" or "None" respectively. And the rule then returns either "O" "M" or "F" and if they choose "None" it returns nothing. It all worked the way I wanted it to except, if someone were to pick "None" in the drop down but then type a number in that phone field, it would return the number with no label for it. So I tried to go back in and add another statement in the rule that says

 

if the field for phone Type (dropdown list) is "None" (I'm using begins with "N" here) and there is "anything" (I'm using is greater than "0" here) entered into that phone number field, then return the word "Error" in front of the number. But it doesn't seem to be working.

 

Ultimately what I would really like to do is say if the field for phone Type (dropdown list) is "None" and something is entered into the phone number field, then return nothing for either the label or the number entered. But I am not knowledgable enough with JS yet to try it.

 

Anyway here is the current code:

 

if (Left(Field("Type 1"),Len(String("O"))) == String("O"))

{

return "<span>" + '< >' + String(" O ") + '< >' + "</span>";

}

if (Left(Field("Type 1"),Len(String("M"))) == String("M"))

{

return "<span>" + '<  >' + String(" M ") + '< >' + "</span>";

}

if (Left(Field("Type 1"),Len(String("F"))) == String("F"))

{

return "<span>" + '< >' + String(" F ") + '< >' + "</span>";

}

if (Left(Field("Type 1"),Len(String("N"))) == String("N"))

{

return "";

}

if ((Left(Field("Type 1"),Len(String("N"))) == String("N")) && (Field("Phone1") > String("o"))) //Here I am trying to say if the phone Type 1 field begins with "N" and the Phone1 field has anything in it then return the word "Error"

{

return "<span>" + String("Error") + "</span>";

}

return "";

Link to comment
Share on other sites

Does anyone see an obvious problem with this code?
Yes.

if ((Left(Field("Type 1"),Len(String("N"))) == String("N")) 
&& (Field("Phone1") > String("o")))  //Here I am trying to say if the phone Type 1 field begins with "N" and the Phone1 field has anything in it then return the word "Error" 

Assuming your code is functional except the error coding, the first thing I see is that you are setting a condition using an "o" [letter o] rather than a "0" [number 0].

Instead of checking to see if the phone variable is larger than 0, I would see if there is anything in the field: (ruling out the option that they actually put a "0" in the box) i.e.

if ((Left(Field("Type 1"),Len(String("N"))) == String("N")) 
&& (Field("Phone1") != "")))

hope this helps

Link to comment
Share on other sites

Actually, the entire rule can be reduced to this:

var TypeCode = ToUpper(Left(Field("Type 1"), 1));
if (TypeCode == "N" && Field("Phone1"))
 return " Error ";
//else
return ' ' + TypeCode +  ' ';

Which I find much easier to follow.

 

The easiest way to determine whether a string is empty or not is to just use it directly as a Boolean (true/false) value, as above. If it's empty, it's false; otherwise it's true.

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Boolean

 

Also, there's no need to use the String keyword (constructor) for string literals. "N" is the same as String("N"). And it's also superfluous to say Len(String("N")) or even Len("N"); that's always going to simply be 1 (one).

 

Also, note that the <span> tags are unnecessary, as they will have absolutely no effect with just regular text to enclose. Also, "< >" is not valid markup; you would need to leave off the angle brackets for a valid " " entity. Actually, since you don't really need any of these tags, I just replaced them with literal space characters, so you can go ahead and UNcheck "Treat returned strings as tagged markup."

Link to comment
Share on other sites

Thanks Lester, I actually had caught the letter "o" instead of the number "0" by the time you responded so nice catch. And I like your idea for looking for anything verses greater than zero, but I just wasn't sure of how to write it. Oddly enough, it didn't work the way you wrote && (Field("Phone1") != ""))) it but when I changed it to && (Field("Phone1") != String(""))) it did. Maybe you can help me to understand why. And the other thing I was trying to figure out is how to script it correctly all in one rule. Because if I create a new rule that just has this new statement in it,

if ((Left(Field("Type 1"),Len(String("N"))) == String("N")) && (Field("Phone1") != String("")))

{

return "<span>" + String("Error") + "</span>";

}

return "";

and name it "ErrorRule" and place it in front of the other "Type1Rule" in the Variable Text Editor so it looks like this:

<<ErrorRule1>><<Type1Rule>><<PhoneNumber1>>

it works fine, but if I try to include it at the end of the Type1 Rule like I showed originally, it doesn't. I must be blending them wrong or something.

Thanks for the help!

Link to comment
Share on other sites

Actually, the entire rule can be reduced to this:

var TypeCode = ToUpper(Left(Field("Type 1"), 1));
if (TypeCode == "N" && Field("Phone1"))
 return " Error ";
//else
return ' ' + TypeCode +  ' ';

Which I find much easier to follow.

 

The easiest way to determine whether a string is empty or not is to just use it directly as a Boolean (true/false) value, as above. If it's empty, it's false; otherwise it's true.

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Boolean

 

Also, there's no need to use the String keyword (constructor) for string literals. "N" is the same as String("N"). And it's also superfluous to say Len(String("N")) or even Len("N"); that's always going to simply be 1 (one).

 

Also, note that the <span> tags are unnecessary, as they will have absolutely no effect with just regular text to enclose. Also, "< >" is not valid markup; you would need to leave off the angle brackets for a valid " " entity. Actually, since you don't really need any of these tags, I just replaced them with literal space characters, so you can go ahead and UNcheck "Treat returned strings as tagged markup."

 

Thanks Dan, but I am going to be honest with you and say that I'm not clear on a lot of what you said. I have never worked with JS until a couple weeks ago when my company got this application and dropped it in my lap to learn. So the reason my codes look the way the do and include a lot to the items you mentioned, is because I start them using the Rule Wizard and get them as coles as I can, then convert to JS and apply the necessary fixes I find to get it the rest of the way. I wish I new a tenth of what you do regarding simply writing the code myself. And I am going to need a lot of coaching for a while until I get the hang of this.

 

Thanks a bunch.

Link to comment
Share on other sites

Thanks Dan, but I am going to be honest with you and say that I'm not clear on a lot of what you said. I have never worked with JS until a couple weeks ago when my company got this application and dropped it in my lap to learn. So the reason my codes look the way the do and include a lot to the items you mentioned, is because I start them using the Rule Wizard and get them as coles as I can, then convert to JS and apply the necessary fixes I find to get it the rest of the way. I wish I new a tenth of what you do regarding simply writing the code myself. And I am going to need a lot of coaching for a while until I get the hang of this.

 

Thanks a bunch.

Sorry about that. The Drag-and-Drop Rule Wizard generates code that works, but that isn't always succinct or easy to follow when it's converted to a JavaScript rule. And sometimes I forget that not everyone is as well-versed in JavaScript as I am.

 

But when I see some logic like, "if the field starts with F, return F, else if the field starts with O, return O, else if the field starts with N, return N, etc.," the pattern jumps out at me, as does a logical reduction that simply says, "return the first letter of the field", which can be represented very concisely in a snippet of JavaScript:

return Left(Field("Type 1"), 1);

And the rest follows from there. The Drag-and-Drop Rule Wizard works really well for some very specific kinds of simple logic, but it can get ungainly if you're trying to do too much with it.

 

Also, instead of converting the entire Drag-and-Drop Wizard rule to JavaScript and then trying to sort through all of the code it generates to modify a part of it, you could just split up the logic. That is, you can leave the Wizard-based rule alone, with just the logic to return the letter you want, and then use the result of that rule in a separate JavaScript rule that simply handles the error case, like so:

if (Rule("MyWizRuleName") == "N" && Field("Phone1"))
    return "Error";
//else
return Rule("MyWizRuleName"); 

And you can keep all the formatting, extra spaces, etc. in the Text Editor and the Rule Wizard, with just the small bit of logic that requires JavaScript actually in code.

 

Unfortunately, I don't know of a way to express the logic, "if the phone Type 1 field begins with 'N' and the Phone1 field has anything in it then return the word 'Error'" in a FusionPro job without writing some JavaScript code.

 

This forum can be an excellent resource to help you with your JavaScript questions, so please keep asking them! You'll get the hang of it.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...