...
Code Block |
---|
language | js |
---|
title | Name Concatenation Rule |
---|
linenumbers | true |
---|
|
var fullName = '';
//Initialize
a variable to store the data
if(data.firstName) //If
there is data, assign it to fullName with a trailing space
fullName = data.firstName + ' ';
if (data.middleInitial)
//If there is data, append it to fullName with a trailing space
fullName += data.middleInitial + ' ';
if (data.lastName)
//If there is data, append it to full name
fullName += data.lastName;
value = fullName.trim(); //Trim off any trailing spaces and assign the data to the 'value' variable |
You can use any Javascript you are comfortable with when author rules like this. The example above shows the basic sequential construction of a string. The same thing can be written in a number of ways. A more developer focused example of this rule is provided below. Note that this code does the exact same thing as the example above, it is just written in a manner that is less understandable to those unfamiliar with Javascript syntax. Use whatever is easier or more understable for you.
Code Block |
---|
language | js |
---|
title | Advanced Name Concatenation Rule |
---|
linenumbers | true |
---|
|
var fullName = (data.firstName ? data.firstName + ' ' : '') + (data.middleInitial ? data.middleInitial + ' ' : '') + (data.lastName || '');
value = fullName.trim(); |
The form example described is available for download here.