Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(window).on('load', function() {
- $.fn.capitalize = function() {
- // words to ignore
- let wordContainAt = '@',
- wordsToIgnore = ['to', 'and', 'the', 'it', 'or', 'that', 'this', 'dos', 'rua', 'das', 'rh'],
- minLength = 2,
- wordUpperCase = ['LTDA', 'S.A'];
- function getWords(str) {
- if (str == undefined) {
- str = 'abc def';
- } else {
- str = str;
- }
- return str.match(/\S+\s*/g);
- }
- this.each(function() {
- let words = getWords(this.value);
- $.each(words, function(i, word) {
- // only continues if the word is not in the ignore list or contains at '@'
- if (word.indexOf(wordContainAt) != -1) {
- words[i] = words[i].toLowerCase();
- } else if (wordUpperCase.indexOf($.trim(word).toUpperCase()) != -1) {
- words[i] = words[i].toUpperCase();
- } else if (wordsToIgnore.indexOf($.trim(word)) == -1 && $.trim(word).length > minLength) {
- words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1).toLowerCase();
- } else {
- words[i] = words[i].toLowerCase();
- }
- });
- if (this.value != '') {
- this.value = words.join('');
- }
- });
- };
- // field onblur with class .title
- $(document).on('blur', '.lower', function() {
- $(this).capitalize();
- }).capitalize();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement