Error with javascrit in separate file

Asked

Viewed 226 times

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:

  • 1

    Take a look in that question.

  • Did the file include correctly?

  • What error you have in the console?

  • The error is in the images.

  • why not use regular expression? for such a simple thing: number = number.replace(/[^0-9]+/g, "");

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 do a validation to check if 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 meant 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 it is keep in mind that an id (ID="txtLinhaDigitavel") must be unique.

  • Hello Kaduamaral, thank you for your help, your solution has solved my problem. If it is not to abuse too much because you made the condition of "IF", I could not understand, if possible you can give a lightening in my Idea. Thank you.

  • Opa @krispim o if is to make sure that the element passed in the parameter is a form field. just to avoid error and interrupt the script where you use the texto.split('') that would make a mistake if the element.value were undefined. That is, it is just a security validation to avoid error in the script. If you always use in element attributes passing the this will never give error, but if at some other time use other conditions searching the element for the class or ID is useful to avoid errors when the elements do not exist in the code...

  • Finally, the excerpt from IF is not essential for the correction of the problem, consider it as a catch. :)

  • Thanks for the clarification Kaduamaral

Browser other questions tagged

You are not signed in. Login or sign up in order to post.