How to call javascript function in asp.net?

1

This is my job

<script>

    function keypressed(obj, e) {
        var tecla = (window.event) ? e.keyCode : e.which;
        var texto = document.getElementById("numeros").value
        var indexvir = texto.indexOf(",")
        var indexpon = texto.indexOf(".")

        if (tecla == 8 || tecla == 0)
            return true;
        if (tecla != 44 && tecla != 46 && tecla < 48 || tecla > 57)
            return false;
        if (tecla == 44) { if (indexvir !== -1 || indexpon !== -1) { return false } }
        if (tecla == 46) { if (indexvir !== -1 || indexpon !== -1) { return false } }
    }

</script>

How can I call it in my TextBox ?

    
asked by anonymous 04.12.2016 / 04:57

3 answers

1

You do not call it in ASP.NET, ASP.NET is backend. You call in the html + javascript. In case the script seems to require onkeydown instead of onkeypress , but as I do not know what the script does, then I can not be sure, an example to use would be:

<input type="text" id="numeros" value="" onkeydown="return keypressed( this , event );" />

Or inside a javascript file:

<script>
window.onload = function() {
      //pega o seu input
      var numeros = document.getElementById("numeros");
      numeros.onkeydown = function(e) {
            keypressed(numeros, e)
      };
};
</script>
    
04.12.2016 / 05:03
0

Markin,

Complementing the answer, in document.getElementById, you must use the ClientID. Asp.net changes the name in some cases when it is sent to the Browser.

document.getElementById("<%=TextBox1.ClientID%>");
    
04.12.2016 / 11:39
0

Try this:

  ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "keypressed(obj, e)", "keypressed(obj, e)", True)


Edited:
Seeing the comments, and I realized that you want to mount a mask, would not it be better to wear some masks ready? Example:
JQuery Mask Plugin
JQuery MoneyMask

    
06.12.2017 / 11:29