The decision accepting exceptions (transferred by parameters):
Copy the code below and use it as follows: $ ('myselector'). maskOwnName (['of', 'on', 'a', 'as', 'at', 'for', 'in', 'to']);
(function($) { $.fn.teste = function(not_capitalize) { not_capitalize = !(not_capitalize instanceof Array)? []: not_capitalize; $(this).keypress(function(e){ if(e.altKey || e.ctrlKey) return; var new_char = String.fromCharCode(e.which).toLowerCase(); if(/[a-zà-ú\.\, ]/.test(new_char) || e.keyCode == 8){ var start = this.selectionStart, end = this.selectionEnd; if(e.keyCode == 8){ if(start == end) start--; new_char = ''; } var new_value = [this.value.slice(0, start), new_char, this.value.slice(end)].join(''); var maxlength = this.getAttribute('maxlength'); var words = new_value.split(' '); start += new_char.length; end = start; if(maxlength === null || new_value.length <= maxlength) e.preventDefault(); else return; for (var i = 0; i < words.length; i++){ words[i] = words[i].toLowerCase(); if(not_capitalize.indexOf(words[i]) == -1) words[i] = PHP.ucfirst(words[i]); } this.value = words.join(' '); this.setSelectionRange(start, end); } }); } $.fn.maskLowerName = function(pos) { $(this).css('text-transform', 'lowercase').bind('blur change', function(){ this.value = this.value.toLowerCase(); }); } $.fn.maskUpperName = function(pos) { $(this).css('text-transform', 'uppercase').bind('blur change', function(){ this.value = this.value.toUpperCase(); }); } })(jQuery);
Doglas Dec 05 '16 at 21:34 2016-12-05 21:34
source share