Note that there are some explanatory texts on larger screens.

plurals
  1. POOne string multiple variables to check with indexOf
    text
    copied!<p>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.</p> <p>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:</p> <pre><code>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(" "); } </code></pre> <p>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.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload