Advertisement
gilcierweb

Função tira espaços e caracteres especiais incluindo acentos

Apr 18th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 0.75 KB | None | 0 0
  1. <script>
  2. // Remove all whitespace
  3. // JavaScript RegEx
  4. var str = " a b    c d e   f g   ";
  5. var newStr = str.replace(/\s+/g, '');
  6. // "abcdefg"
  7.  
  8. var names = "TESTE_TESTE-2014ÇẼẼ__";
  9. var regex = new RegExp("\W");
  10.  
  11. names.replace(regex, "");
  12. var newStr = names.replace(/[^A-Za-z0-9._]/g, '');
  13. var newStr = names.replace(/[^A-Za-z0-9._]/g,"");
  14.  
  15. console.log(newStr);
  16.  
  17. $.fn.tiraEspacosCharSpecial = function(string) {
  18. var newStr = string.replace(/[^A-Za-z0-9._]/g,"");
  19.   return newStr;
  20. };
  21.  
  22. $(this).tiraEspacosCharSpecial(names);
  23.  
  24. $('#nome').keyup(function(){
  25.     $.fn.tiraEspacosCharSpecial($( this ).val());  
  26.     $(this).val($.fn.tiraEspacosCharSpecial($(this).val()));
  27.     console.log($.fn.tiraEspacosCharSpecial($(this).val()));
  28. }).keyup();
  29.  
  30. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement