1

I have the following code which works well to convert data entered into the "Firstname" field in our data enrollment software application to uppercase and return the converted value back to the application.

However, it doesn't handle names with "-", "'" or spaces in them, for example Anne-Marie, Jean Jacques, O’Brian. Could someone please help me in adding a few lines of code to handle these name types as well as preserving my original code which works for standard names without these characters in? Here is my code.

var tc_event = changeValue();

function changeValue() {
    // Parse the JSON string for script information.
    var tcInfo = JSON.parse(TC_Info);
    /* FROM ENGINEERING: The “TC_Info” variable contains the user id and IP address of the user running the script. 
     * We have at least one customer that wanted that information */
    var userId = tcInfo.userId;
    var ipAddress = tcInfo.ipAddress;

    // Parse the JSON string for fields and properties.
    var tcData = JSON.parse(TC_Event);
    // The following several lines of code loops over the workflow fields passed in to the script and saves references to the fields named “Lastname” and “LastnameUppercase”
    var Lastname, LastnameUppercase, Firstname, Firstname1stUppercase;
    // Iterate through parsed JSON.
    for (var index in tcData) {
        // Fetch each field i.e each key/value pair.
        var field = tcData[index];
        // Find the fields to process.
        if (field.name === 'Lastname') {
            Lastname = field;
        } else if (field.name === 'LastnameUppercase') {
            LastnameUppercase = field;
        } else if (field.name === 'Firstname') {
            Firstname = field;
        } else if (field.name === 'Firstname1stUppercase') {
            Firstname1stUppercase = field;
        } else if (field.name === 'PersNr') {
            PersNr = field;
        } else if (field.name === 'TikNr') {
            TikNr = field;
        }
    }

    // Were the fields found? If so, proceed.
    if (Lastname && LastnameUppercase && Firstname && Firstname1stUppercase && PersNr && TikNr) {

        // This line of code states the LastnameUppercase field value will be the Lastname field value in uppercase
        LastnameUppercase.value = Lastname.value.toUpperCase();

        Firstname1stUppercase.value = Firstname.value.charAt(0).toUpperCase() + Firstname.value.slice(1);

        var strLtr = PersNr.value.substring(0, 2);
        var strNum = PersNr.value.substring(2, 6);
        if (strLtr === '00') {
            strLtr = 'A';
        } else if (strLtr === '01') {
            strLtr = 'M';
        } else if (strLtr === '31') {
            strLtr = 'B';
        } else if (strLtr === '71') {
            strLtr = 'F';
        }

        TikNr.value = strLtr + strNum;
    }

    // Return the updated fields and properties.
    return JSON.stringify(tcData);
}
Božo Stojković
  • 2,893
  • 1
  • 27
  • 52
  • 1
    Most of the code you posted seems to be irrelevant to the question asked. Please read [this](http://stackoverflow.com/help/mcve) and amend your question appropriately. – Federico klez Culloca Mar 01 '18 at 15:42
  • It might be worth reading https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ as it give a wider perspective on handling names. This is one of a larger lists of https://github.com/kdeldycke/awesome-falsehood – Jason Aller Mar 01 '18 at 21:58
  • Possible duplicate of [Convert string to title case with JavaScript](https://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript) – Michel Mar 02 '18 at 07:44

4 Answers4

1

This will capitalize both the firstName that do not contain symbols and the ones that do:

function capitalize(name) {
  let capitalizedName = '';
  const nameSplit = name.split(/\W/g);
  const symbols = name.match(/\W/g);

  for(let i = 0; i< nameSplit.length; i++) {
    capitalizedName += nameSplit[i][0].toUpperCase() +
      nameSplit[i].slice(1)
    if(i < nameSplit.length -1)  capitalizedName += symbols[i];
  }
  return capitalizedName
}
Marino Di Clemente
  • 3,120
  • 1
  • 23
  • 24
Mass C
  • 41
  • 4
0

I have used this function successfully:

function capitalizeName(str) {
    var result = str.replace(/\w\S*/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); });
    return result.replace(/\s\s+/g, ' ');
}

calling the function:

capitalName = capitalizeName(lowerCaseName)
0

Looks like you should change

Firstname1stUppercase.value = Firstname.value.charAt(0).toUpperCase() + Firstname.value.slice(1);

to

var delimiter = ''; //char value

if(Firstname.value.indexOf(' ') != -1){ //name has a space
  delimiter = ' ';
}else if(Firstname.value.indexOf('-') != -1){ //name has -
  delimiter = '-';
}else if(Firstname.value.indexOf('\'') != -1){ //name has a '
  delimiter = '\'';
}

Firstname1stUppercase.value = Firstname.split(delimeter).map(function(val) {
  return val.charAt(0).toUpperCase() + val.slice(1);
}).join(delimeter);

The last line is what you were doing but written for any separating character be it a space, apostrophe, or hyphen.

John
  • 101
  • 5
-1

You could split by non alphabetic letters, like this:

text.split(/[^A-Za-z]/);

inspired from here: Split string by non-alphabetic characters

Now, let's implement the function you need:

function myUpperCase(input) {
    var parts = input.split(/[^A-Za-z]/);
    var output = parts[0];
    for (var i = 1; i < parts.length; i++) {
        if (parts[i].length) parts[i] = parts[i][0].toUpperCase() + parts[i].substring(1);
        output += input[output.length] + parts[i];
    }
    return output;
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175