Advertisement
Guest User

402022

a guest
Aug 6th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(window).on('load', function() {
  2.     $.fn.capitalize = function() {
  3.         // words to ignore
  4.         let wordContainAt = '@',
  5.             wordsToIgnore = ['to', 'and', 'the', 'it', 'or', 'that', 'this', 'dos', 'rua', 'das', 'rh'],
  6.             minLength = 2,
  7.             wordUpperCase = ['LTDA', 'S.A'];
  8.  
  9.         function getWords(str) {
  10.             if (str == undefined) {
  11.                 str = 'abc def';
  12.             } else {
  13.                 str = str;
  14.             }
  15.             return str.match(/\S+\s*/g);
  16.         }
  17.         this.each(function() {
  18.             let words = getWords(this.value);
  19.             $.each(words, function(i, word) {
  20.                 // only continues if the word is not in the ignore list or contains at '@'
  21.                 if (word.indexOf(wordContainAt) != -1) {
  22.                     words[i] = words[i].toLowerCase();
  23.                 } else if (wordUpperCase.indexOf($.trim(word).toUpperCase()) != -1) {
  24.                   words[i] = words[i].toUpperCase();
  25.                 } else if (wordsToIgnore.indexOf($.trim(word)) == -1 && $.trim(word).length > minLength) {
  26.                     words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1).toLowerCase();
  27.                 } else {
  28.                     words[i] = words[i].toLowerCase();
  29.                 }
  30.             });
  31.             if (this.value != '') {
  32.                 this.value = words.join('');
  33.             }
  34.         });
  35.     };
  36.  
  37.     // field onblur with class .title
  38.     $(document).on('blur', '.lower', function() {
  39.         $(this).capitalize();
  40.     }).capitalize();
  41. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement