2

I have written a little JQuery / Javascript add on for our form, that takes a single full name input and breaks it into first and last name components. It opens a modal if there are three or more names in the input and asks which combo is correct.

My next step is finding and stripping any suffix that may have been entered such as Jr, Sr, III, etc. I am currently stripping off the last four characters and checking them with indexOf to see if they contain a suffix string (Jr, Sr, III, etc). But each line checks only one possible suffix and I am wondering is there is some js magic that will check multiple suffixs in one line. My current code is below:

var nameVal = $('#name').val(); var suffix = nameVal.slice(-4); if (suffix.toLowerCase().indexOf(" jr") != -1) { var nameSplit = nameVal.slice(0, -3).split(" "); } elseif (suffix.toLowerCase().indexOf(" iii") != -1) { var nameSplit = nameVal.slice(0, -4).split(" "); } else { var nameSplit = nameVal.split(" "); } 

I can always do the good old || and keep adding extra (suffix.toLowerCase().indexOf(" jr") != -1) with a different indexOf value, but I am hoping to keep the code more compact if possible, my "little" script is already 3k.

Once I get this sorted the last step will be figuring out how to retain the last name value, so that further down the form when other names are entered and the script is called again it can check to see if the selected last name matches the new entry and bypass the modal pop up.

2
  • you will have extremely unexpected results when defining variables inside blocks like that. learn more about scope and hoisting here Commented Mar 17, 2012 at 0:46
  • @jbabey: It's pretty hard to get into a situation where that is a problem, unless you are using variables before they are declared in code. Moreover, most people define their loops using for (var i=0; i < len; i++). Hoisting is not the problem here, for clarity, the var nameSplitshould be above the if statement Commented Mar 17, 2012 at 0:56

3 Answers 3

2

You can use a regular expression. Try something like this;

nameVal = nameVal.replace(/ (jr|sr|I?II)$/gi, ""); 

In more detail;

(jr|sr|I?II) = jr or sr or II or III $ = at the end of line /i = case insensitive /g match globally 
Sign up to request clarification or add additional context in comments.

5 Comments

I knew there had to be a better way. Thanks, that worked like a charm.
not entirely sure about the OP's intent here, but a /g may also be in order
@Tom I changed it a little bit. Now it requires a space before suffixes and handles both II and III (I?II means optional I, followed by II).
@Tom /g means a global match. So, it would change all matches to "". However, since we match the end of line, it would only affect multi line strings in this case.
Thanks again. We actually are looking to strip all sorts of suffix, Jr, Sr, II, III, IV, PHD, MD, etc. But adding them to the replace will be much easier than my old method.
0

Probably best to use regexps for this, for example:

var names = [ "Joe Jr.", "Mark Sr.", "Loui III", "Mark Lockering", ]; var suffixRes = [ /Jr\.$/, /Sr\.$/, 'III', ]; $(names).each(function(i, name) { var str = name; $(suffixRes).each(function(j, suffixRe) { str = str.replace(suffixRe, ''); }); console.log(str); }); 

Live example:

2 Comments

I am only looking to deal with one name at a time, that was entered into a form input on our webpage.
Agree, what I put is to show the output for different inputs, nothing else. Just use the gist of the function (i.e. remove the $(names).each(...) call around the main function).
0

In this case I usually make an array of values, (because I'm not good with regex)

var suffixArr = [' jr',' iii', ' ii']; //then run a loop for(var i = 0; i < suffixArr.length;i++){ if(suffixArr[i].toLowerCase().indexOf(suffixArr[i]) != -1){ nameSplit = nameVal.slice(0, - suffixArr[i].length).split(" "); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.