Answered
I have a form that has LastName, FirstName and MI fields on it. I have another section of the form that needs the LastName + FirstName + MI on it. All I want is a simple script that will fill the new form field FullName with the combined text of the other three fields. I can't find the solution out here and it has been a while since I tried to mess with any scripting at all. Any simple solution here?
Field Names:
LastName
FirstName
MI
FullName
Thanks,
SFC Woods
// Concatenate up to 3 strings with an optional separator where needed
function fillin(s1, s2, s3, sep) {
// computed value to determine which strings to combine
var test = 0; // binary: 000
var string = "";
// convert passed parameters to strings
s1 = s1.toString();
s2 = s2.toString();
s3 = s3.toString();
sep = sep.toString();
// compute a unique value to deternine what specific strings to combine
if (s1 != "") test += 1; // binary: test + 001
if (s2 != "") test += 2; // binary: test + 010
if (s3 != "") test += 4; // binary: test + 100
// select combination of strings based on computed value
switch(test.toString()) {
case "0": // binary: 000
string = "";
break;
case "1": // binary: 001
string = s1;
break;
case "2": // binary: 010
string = s2;
case "3": // binary: 011
string = s1 + sep + s2;
break;
case "4": // binary: 100
string = s3;
break;
case "5": // binary: 101
string = s1 + sep + s3;
break;
case "6": // binary 110
string = s2 + sep + s3;
break;
case "7": // binary: 111
string = s1 + sep + s2 + sep + s3;
break;
} end switch text
return string;
} // end function fillin
And the custom calculation srcirpt for the name fields:
// Name only
function doFullName() {
var first = this.getField("name.first");
var initial= this.getField("name.initial");
var last = this.getField("name.last");
event.value = fillin(first.value, initial.value, last.value, " ");
}
doFullName();
And the full name with prefix, suffix and nickname:
// All name fields concatenated, with some nice formatting.
function doCompleteName() {
var prefix = this.getField("name.prefix");
var first = this.getField("name.first");
var initial = this.getField("name.initial");
var last = this.getField("name.last");
var suffix = this.getField("name.suffix");
var nickname= this.getField("name.nickname");
var result1 = fillin(prefix.value, first.value, initial.value, " ");
if (nickname.value != "")
var result2 = fillin(last.value, suffix.value, "\"" + nickname.value + "\"", " ");
else
var result2 = fillin(last.value, suffix.value, "", " ");
event.value = fillin(result1, result2, "", " ");
}
doCompleteName();
George Kaiser