Bom dia =D
Hoje resolvi postar um script que faz a restrição de inserção em campos html do tipo input text, com esse script ativado o usuário pode apenas digitar números.
//tam = Tamanho do campo //fld = input //e = evento function ConsisteNumerico(tam, fld, e) { var key = ''; var i = 0; var len = 0; var strCheck = '0123456789'; var aux = ''; var whichCode = (window.Event) ? e.which : e.keyCode; if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true; // Enter key = String.fromCharCode(whichCode); // Get key value from key code if (strCheck.indexOf(key) == -1) return false; // Not a valid key len = tam -1; aux = ''; for(; i < len; i++) if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i); aux += key; fld.value = ''; fld.value += aux; return false; }[/code]</pre> <p>Para ativar o script basta inserir no evento onKeyPress do input a chamada da função, conforme exemplo abaixo:</p> <p><input type="text" size="5" name="testeNumerico" onKeyPress="return(ConsisteNumerico('5',this,event))" value=""/>
Jean,
Muito legal a função, me ajudou muito!
Obrigada.