How to simplify code, which adds spaces and commas if field value is not empty? var searchDetails = "";
searchDetails += $("#last-name").val();
searchDetails += searchDetails != "" ? " "+$("#first-name").val() : $("#first-name").val();
searchDetails += searchDetails != "" ? " "+$("#middle-name").val() : $("#middle-name").val();
if ($("#dob-dd").val() != "" || $("#dob-mm").val() != "" || $("#dob-yyyy").val() != "") {
    searchDetails += searchDetails != "" ? ", " : "";
    searchDetails += "date of birth: ";
    searchDetails += ($("#dob-dd").val() != "" ? $("#dob-dd").val() : "xx") + ".";
    searchDetails += ($("#dob-mm").val() != "" ? $("#dob-mm").val() : "xx") + ".";
    searchDetails += ($("#dob-yyyy").val() != "" ? $("#dob-yyyy").val() : "xx") + ".";
}
if ($("#passport-id").val() != "") {
    searchDetails += searchDetails != "" ? ", " : "";
    searchDetails += "passport " + $("#passport-id").val();
}


searchDetails should contain values like (depending on what user has input):

lastName firstName middleName, date of birth dd.mm.yyyy, passport 123456789
firstName middleName, date of birth dd.xx.yyyy
lastName firstName, date of birth dd.mm.xxxx
date of birth dd.mm.xxxx, passport 123456789
lastName firstName middleName
passport 123456789
Firstly, I'd create a helper function for the != "" check:

function isEmptyString(value) {
    if (value == "") {
        return true;
    }
    return false;
}


It would make the code easier to read.

Then I'd extract out a function for sturctures like ($("#dob-dd").val() != "" ? $("#dob-dd").val() : "xx") + ".":

function wrapDateOfBirth(id) {
    return ($(id).val() != "" ? $(id).val() : "xx") + ".";
}


and structures like searchDetails != "" ? " "+$("#first-name").val() : $("#first-name").val();:

function wrapSearchDetails($value, $id) {
    return $value != "" ? " " + $($id).val() : $($id).val();
}


They would remove some duplication.

Furthermore, the wrapSearchDetails function could be written like this too:

function wrapSearchDetails(value, id) {
    return (isEmptyString(value) ? "" : " ") + $(id).val();
}


The wrapDateOfBirth function also could be simplified:

function wrapDateOfBirth(id) {
    var value = $(id).val();
    return (isEmptyString(value) ? "xx" : value) + ".";
}


Finally, a getDefaultIfEmpty function also could improve the readability of the wrapDateOfBirth function:

function getDefaultIfEmpty(value, defaultValue) {
    if (isEmptyString(value)) {
        return defaultValue;
    }
    return value;
}

function wrapDateOfBirth(id) {
    var value = $(id).val();
    return getDefaultIfEmpty(value, "xx") + ".";
}


(I have not tested the code, please feel free to edit if you find any typo/etc.)