0
I have a function javascript
to validate number-only input in a field text
when onkeypress
, onKeyUp
, onBlur
and onChange
, when I put this code on the page works, but I have several other pages that need to be validated by this function, so it was centralized this function in a single file javascript
, However, when I center the function does not work.
This is the code I’m using
Function:
function SomenteCaracteresNumericos(idControle) {
var texto = document.getElementById(idControle).value;
var textoSeparado = texto.split('');
var numeros = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
for (var i = 0; i < textoSeparado.length; i++) {
var teclaValida = false;
for (var j = 0; j < numeros.length; j++) {
if (numeros[j] == textoSeparado[i]) {
teclaValida = true;
}
}
if (!teclaValida)
textoSeparado[i] = '';
}
document.getElementById(idControle).value = textoSeparado.join('');
}
This is the for
that I use to call the function
<asp:TextBox ID="txtLinhaDigitavel" runat="server" Width="390px" onFocus="javascript:this.select();" onkeypress="javascript:SomenteCaracteresNumericos(this.id);" onKeyUp="javascript:SomenteCaracteresNumericos(this.id);" onBlur="javascript:SomenteCaracteresNumericos(this.id);" onChange="javascript:SomenteCaracteresNumericos(this.id);"></asp:TextBox>
This is the error on the page:
Take a look in that question.
– Renan Gomes
Did the file include correctly?
– durtto
What error you have in the console?
– Marconi
The error is in the images.
– krispim
why not use regular expression? for such a simple thing:
number = number.replace(/[^0-9]+/g, "");
– Ivan Ferrer