máscaras para formulários


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <script language="javascript">
  2. /*Função Pai de Mascaras*/
  3. function Mascara(o,f){
  4. v_obj=o
  5. v_fun=f
  6. setTimeout("execmascara()",1) }
  7. /*Função que Executa os objetos*/
  8. function execmascara(){
  9. v_obj.value=v_fun(v_obj.value) }
  10. /*Função que Determina as expressões regulares dos objetos*/
  11. function leech(v){
  12. v=v.replace(/o/gi,"0")
  13. v=v.replace(/i/gi,"1")
  14. v=v.replace(/z/gi,"2")
  15. v=v.replace(/e/gi,"3")
  16. v=v.replace(/a/gi,"4")
  17. v=v.replace(/s/gi,"5")
  18. v=v.replace(/t/gi,"7")
  19. return v }
  20. /*Função que padroniza telefone (11) 4184-1241*/
  21. function Telefone(v){
  22. v=v.replace(/\D/g,"")
  23. v=v.replace(/^(\d\d)(\d)/g,"($1) $2")
  24. v=v.replace(/(\d{4})(\d)/,"$1-$2")
  25. return v }
  26. /*Função que padroniza CPF*/
  27. function Cpf(v){
  28. v=v.replace(/\D/g,"")
  29. v=v.replace(/(\d{3})(\d)/,"$1.$2")
  30. v=v.replace(/(\d{3})(\d)/,"$1.$2")
  31. v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2")
  32. return v }
  33. /*Função que padroniza CEP*/
  34. function Cep(v){
  35. v=v.replace(/D/g,"")
  36. v=v.replace(/^(\d{5})(\d)/,"$1-$2")
  37. return v }
  38. /*Função que padroniza DATA*/
  39. function Data(v){
  40. v=v.replace(/\D/g,"")
  41. v=v.replace(/(\d{2})(\d)/,"$1/$2")
  42. v=v.replace(/(\d{2})(\d)/,"$1/$2")
  43. return v }
  44. /*Função que padroniza VALOR MONETARIO - R$*/
  45. function MaskMonetario(v){
  46. v=v.replace(/\D/g,"");
  47. v=v.replace(/(\d{2})$/,",$1");
  48. v=v.replace(/(\d+)(\d{3},\d{2})$/g,"$1.$2");
  49. var qtdLoop = (v.length-3)/3; var count = 0;
  50. while (qtdLoop > count){ count++;
  51. v=v.replace(/(\d+)(\d{3}.*)/,"$1.$2");
  52. }v=v.replace(/^(0)(\d)/g,"$2");
  53. return v }
  54. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.