I have a javascript
function to validate only numbers entry in a text
field when it is onkeypress
, onKeyUp
, onBlur
and onChange
, when I put this code on the page it works, several other pages that need to be validated by this function, then this function was centralized in a single javascript
file, however, when I centralize 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 that occurs on the page: