Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. // Scenario:
  2. // Users enter name in all lowercase or all uppercase it fixes the case.
  3. // This isn't 100% suitable for all names but eh whatever!
  4. // Attempts to resepect intentional ' and " and intentional multiple case.
  5.  
  6. $.fn.capitalize = function() {
  7. $(this).blur(function(event) {
  8. var box = event.target;
  9. var txt = $(this).val();
  10. var lc = txt.toLocaleLowerCase();
  11. var startingWithLowerCaseLetterRegex = new RegExp(String.fromCharCode(92)+"b([a-z])", "g");
  12. if (!/([-'"])/.test(txt) && txt === lc || txt === txt.toLocaleUpperCase()) {
  13. var stringStart = box.selectionStart;
  14. var stringEnd = box.selectionEnd;
  15. $(this).val(lc.replace(startingWithLowerCaseLetterRegex, function(c) { return c.toLocaleUpperCase() }).trim());
  16. box.setSelectionRange(stringStart, stringEnd);
  17. }
  18. });
  19. return this;
  20. }
  21.  
  22. // Usage:
  23. $('input[type=text].capitalize').capitalize();
  24.  
  25. // Needed it for a contact form 7 in wordpress, thats why it looks weird and has no comments as it was stripping my \ character or changing new lines to <br> gah!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement