Error with javascrit in separate file

0

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:

    
asked by anonymous 24.08.2015 / 15:37

1 answer

0

Instead of passing the ID, pass the element to the function:

<asp:TextBox 
    ID="txtLinhaDigitavel" 
    runat="server" 
    Width="390px" 
    onFocus="javascript:this.select();" 
    onkeypress="javascript:SomenteCaracteresNumericos(this);" 
    onKeyUp="javascript:SomenteCaracteresNumericos(this);" 
    onBlur="javascript:SomenteCaracteresNumericos(this);" 
    onChange="javascript:SomenteCaracteresNumericos(this);"
></asp:TextBox>

And make a validation to verify that the function parameter is valid.

function SomenteCaracteresNumericos(element) {

    if (!(typeof element === "object" && element.value !== undefined)){
        console.error('Elemento não encontrado.');
        return;
    }

    var texto = element.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] = '';
    }
    element.value = textoSeparado.join('');
}

Note: I'm not sure what you mean by:

  

This is the for that I use to call the function

It was to talk about the TextBox excerpt, I hope not, but if you keep in mind that an id ( ID="txtLinhaDigitavel" ) should be unique.

    
24.08.2015 / 16:16