Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

shumi

Só aceitar numeros nessa função

Recommended Posts

Pessoal,

Desculpa o pé no saco que estou hoje!

É que tenho que adiantar pra amanhã e eu deixei os JS pro fim e agora não consigo sair do lugar...

Eu tenho essa função:

<script language="JavaScript" type="text/javascript">function FormataValor(campo,tammax,teclapres) {var tecla = teclapres.keyCode;vr = document.getElementById(campo).value;vr = vr.replace( "/", "" );vr = vr.replace( "/", "" );vr = vr.replace( ",", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );vr = vr.replace( ".", "" );tam = vr.length;if (tam < tammax && tecla != 8){ tam = vr.length + 1; }if (tecla == 8 ){ tam = tam - 1; }if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){if ( tam <= 2 ){document.Cadastro[campo].value = vr; }if ( (tam > 2) && (tam <= 5) ){document.Cadastro[campo].value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ); }if ( (tam >= 6) && (tam <= 8) ){document.Cadastro[campo].value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }if ( (tam >= 9) && (tam <= 11) ){document.Cadastro[campo].value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }if ( (tam >= 12) && (tam <= 14) ){document.Cadastro[campo].value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ); }if ( (tam >= 15) && (tam <= 17) ){document.Cadastro[campo].value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam );}}}</script>

Uso:

<input name="salario" type="text" id="salario" onKeyDown="FormataValor(this.id, 13, event)" size="10">
Só que se eu coloco letras nesse campo aceita.

Como faço para não aceitar letras???

Compartilhar este post


Link para o post
Compartilhar em outros sites

Que raiva nunca consigo de prima!

Cara não funfou comigo, onde que fiz a burrada?

 

var valores = {  valor1: "",  valor2: "",	valor3: ""}function formataValor(campo, tammax, event) {this.getKey = function(event) {return event?(event.keyCode?event.keyCode:(event.which?event.which:event.charCode)):null;}this.replaceNumpad = function(key) {if (key > 95 && key < 106) return key-96;else return String.fromCharCode(key);}var key = this.getKey(event);var caract = replaceNumpad(key);var tam = valores[campo].length;if (key != 8) {var rule = /[^0-9]/.test(caract);if (tam < tammax)if (!rule) {valores[campo] += caract;tam++;}}else{valores[campo] = valores[campo].substring(0,valores[campo].length-1);tam--;}if ( tam <= 2 )document.form[campo].value = valores[campo];if ( (tam > 2) && (tam <= 5) )document.form[campo].value = valores[campo].substr( 0, tam - 2 ) + ',' + valores[campo].substr( tam - 2, tam );if ( (tam >= 6) && (tam <= 8) )document.form[campo].value = valores[campo].substr( 0, tam - 5 ) + '.' + valores[campo].substr( tam - 5, 3 ) + ',' + valores[campo].substr( tam - 2, tam );if ( (tam >= 9) && (tam <= 11) )document.form[campo].value = valores[campo].substr( 0, tam - 8 ) + '.' + valores[campo].substr( tam - 8, 3 ) + '.' + valores[campo].substr( tam - 5, 3 ) + ',' + valores[campo].substr( tam - 2, tam );if ( (tam >= 12) && (tam <= 14) )document.form[campo].value = valores[campo].substr( 0, tam - 11 ) + '.' + valores[campo].substr( tam - 11, 3 ) + '.' + valores[campo].substr( tam - 8, 3 ) + '.' + valores[campo].substr( tam - 5, 3 ) + ',' + valores[campo].substr( tam - 2, tam );if ( (tam >= 15) && (tam <= 17) )document.form[campo].value = valores[campo].substr( 0, tam - 14 ) + '.' + valores[campo].substr( tam - 14, 3 ) + '.' + valores[campo].substr( tam - 11, 3 ) + '.' + valores[campo].substr( tam - 8, 3 ) + '.' + valores[campo].substr( tam - 5, 3 ) + ',' + valores[campo].substr( tam - 2, tam );if (key != 9) return false;}

Uso:

<form name="Cadastro" id="Cadastro" method="post" action="requisicao_pessoal_valida.asp">

e

<input name="salario" type="text" id="salario" size="10" onKeyDown="return formataValor(this.name, 13, event)" style="text-align: right">

Valeu!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Bingo!!!!!!!Valeu!!!!

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.