dcsimg
www.webdeveloper.com
Results 1 to 12 of 12

Thread: Javascript that automatically capitalizes certain letters such as "McC" or "O'Shea"

  1. #1
    Join Date
    Jul 2015
    Posts
    2

    Javascript that automatically capitalizes certain letters such as "McC" or "O'Shea"

    Hi there,

    I'm currently working with forms that use javascript - toUppercase() function for the last name. However, there are exceptions I run into. A user may have last names such as:

    McCormick
    O'Sullivan
    O'Shea

    I need assistance creating javascript that automatically capitalizes certain letters such as last names beginning with "Mc" or "O'"


    Could someone help me with this?

    Thanks so much!

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,649

    Lightbulb

    Might not be perfect as I've only tested the supplied names ...

    PHP Code:
    <!DOCTYPE html>
    <
    html lang="en">
    <
    head>
    <
    meta charset="UTF-8" />
    <
    titleModify certain names </title>
    </
    head>
    <
    body>

    <
    script type="text/javascript">
    // For: http://www.webdeveloper.com/forum/showthread.php?313787-Javascript-that-automatically-capitalizes-certain-letters-such-as-quot-McC-quot-or-quot-O-Shea-quot

    var specialNames = ["McC","O'S"];
    var 
    exampleNames "Law Firm of: mccormick, o'sullivan and o'shea";

    function 
    checkNames(origStr) {
      var 
    alteredStr origStr.toLowerCase(), poslen;
      for (var 
    i=0i<specialNames.lengthi++) {
        do {
          
    pos alteredStr.indexOf(specialNames[i].toLowerCase());
          if (
    pos != -1) {
            
    len specialNames[i].length;
            
    origStr origStr.substr(0,pos)+specialNames[i]+origStr.substr(pos+len);
            
    alteredStr alteredStr.substr(0,pos)+specialNames[i]+alteredStr.substr(pos+len);  // alert(alteredStr);
          
    }
        } while (
    pos != -1);
      }
      return 
    origStr;
    }
    alert(exampleNames+'\n\n'+checkNames(exampleNames));

    </script>
    </body>
    </html> 

  3. #3
    Join Date
    Jul 2015
    Posts
    2
    Thanks so much!

    Quote Originally Posted by JMRKER View Post
    Might not be perfect as I've only tested the supplied names ...

    PHP Code:
    <!DOCTYPE html>
    <
    html lang="en">
    <
    head>
    <
    meta charset="UTF-8" />
    <
    titleModify certain names </title>
    </
    head>
    <
    body>

    <
    script type="text/javascript">
    // For: http://www.webdeveloper.com/forum/showthread.php?313787-Javascript-that-automatically-capitalizes-certain-letters-such-as-quot-McC-quot-or-quot-O-Shea-quot

    var specialNames = ["McC","O'S"];
    var 
    exampleNames "Law Firm of: mccormick, o'sullivan and o'shea";

    function 
    checkNames(origStr) {
      var 
    alteredStr origStr.toLowerCase(), poslen;
      for (var 
    i=0i<specialNames.lengthi++) {
        do {
          
    pos alteredStr.indexOf(specialNames[i].toLowerCase());
          if (
    pos != -1) {
            
    len specialNames[i].length;
            
    origStr origStr.substr(0,pos)+specialNames[i]+origStr.substr(pos+len);
            
    alteredStr alteredStr.substr(0,pos)+specialNames[i]+alteredStr.substr(pos+len);  // alert(alteredStr);
          
    }
        } while (
    pos != -1);
      }
      return 
    origStr;
    }
    alert(exampleNames+'\n\n'+checkNames(exampleNames));

    </script>
    </body>
    </html> 

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,649

    Lightbulb

    You're most welcome.
    Happy to help.
    There may be shorter/faster versions using regular expressions (regex).
    Good Luck!


    EDIT:
    Alternate version with RegEx. Appears to work the same for values tested ...

    PHP Code:
    <!DOCTYPE html>
    <
    html lang="en">
    <
    head>
    <
    meta charset="UTF-8" />
    <
    titleModify certain names </title>
    </
    head>
    <
    body>

    <
    script type="text/javascript">
    // For: http://www.webdeveloper.com/forum/showthread.php?313787-Javascript-that-automatically-capitalizes-certain-letters-such-as-quot-McC-quot-or-quot-O-Shea-quot

    var specialNames = ["McC","O'S","MacD","McD"];
    var 
    exampleNames "Law Firm of: macDonald, mccormick, mcdavid, o'sullivan and o'shea";

    function 
    checkNames(origStr) {
      var 
    myRegExp;
      for (var 
    i=0i<specialNames.lengthi++) {
        
    myRegExp = new RegExp(specialNames[i],'ig');
        
    origStr origStr.replace(myRegExp,specialNames[i]);
      } return 
    origStr;
    }

    alert(exampleNames+'\n\n'+checkNames(exampleNames));

    </script>
    </body>
    </html> 
    Last edited by JMRKER; 07-09-2015 at 03:32 PM. Reason: Alternative attempt

  5. #5
    Join Date
    Nov 2002
    Location
    Flint, Michigan, USA
    Posts
    644
    What about names like MacGregor? What do you expect when the input word is "machinery"?

  6. #6
    Join Date
    Oct 2013
    Posts
    1,240
    Quote Originally Posted by jalarie View Post
    What do you expect when the input word is "machinery"?
    Ha! Years ago (back in the '70s) there was a book titled "Sir Machinery" (read MacHinery ). Read it as a youth.
    http://www.goodreads.com/book/show/2....Sir_Machinery

    Good point though. Just when you think you've covered all the bases, the exception to the rule ruins everything.
    Last edited by Kevin2; 07-23-2015 at 03:08 PM. Reason: Added link

  7. #7
    Join Date
    Nov 2002
    Location
    Flint, Michigan, USA
    Posts
    644
    I thought this looked like a fun project, so I wrote it in PERL, translated it into JavaScript, and got it to work with: D Eath, D'eath, d'Annunzio, da Vinci, de' Medici, de Medicis, DeLorean, du Barry, DuPont, Macedonia, Machinery, Macgregor, Mack, Macready, MacDonald, M'Carthy, M'grego, McDonald, Mac Dowell, MacLeod, MacPhall, Macpherson, Macquarie, O'Connor, O'reilly, van der Waals, von Trapp, Smith IV, Smith Sr , Smith Sr. , Smith Jr , Smith Jr., AvariJade, D'Jahnte, MarZette, MyAsia, Damarion, and DaMarion.

    Those last two (Damarion and DaMarion) are boy and girl. Yes, they BOTH come out correctly.

    This is all done with regular expressions. And it's not all that difficult.

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,649
    How many regular expressions did you use?

  9. #9
    Join Date
    Aug 2015
    Posts
    1
    I also have the need to use your voice without knowing lam

  10. #10
    Join Date
    Nov 2002
    Location
    Flint, Michigan, USA
    Posts
    644
    Quote Originally Posted by JMRKER View Post
    How many regular expressions did you use?
    I have 30 regular expressions to handle all variations of the names that I listed.

  11. #11
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,649
    Quote Originally Posted by jalarie View Post
    ...

    Those last two (Damarion and DaMarion) are boy and girl. Yes, they BOTH come out correctly.

    This is all done with regular expressions. And it's not all that difficult.
    What do the last 2 regular expressions look like and how are they different?

  12. #12
    Join Date
    Nov 2002
    Location
    Flint, Michigan, USA
    Posts
    644
    Some names, like those last two, are handled individually. To get those to work, I checked to make sure that the entered name is not all capitals, but contains at least one capital after the first character, and then return it as-is. If you enter "DaMarion" that's what comes back. Any other version, all caps, all lower-case, or inital capital only, produces "Damarion".

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles