How to call javascript function in Asp.net?

Asked

Viewed 2,917 times

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 you in mine TextBox?

  • Note: I can get it on an html page like this: <input type="text"id="numbers"value=""onkeypress="returnkeypressed( this , Event );"/> But I can’t get it on an Asp.net. what may be going on?

  • This ASP.NET is Webforms or MVC?

3 answers

1

You don’t call it on Asp.net, Asp.net is back-end. You call it in html+javascript. In case the script seems to need onkeydown instead of onkeypress, but since I don’t know what the script does, so I can’t 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>
  • My script only allows typing numbers and a comma. In another textbox I called a function like this: <Asp:Textbox ID="txtTotalEstoque" runat="server" Maxlength="7" onkeypress="Return Permitesomentenumeros(Event);" Text='<%# Bind("Totalstock") %>'></Asp:Textbox>

  • Why doesn’t it work with the other script?

  • Did not generate any error no

  • <Asp:Textbox ID="txtPreco" runat="server" class="txtPreco"; Maxlength="7" onkeypress="Return keypressed( this , Event );" Text='<%# Bind("Price") %>'></Asp:Textbox>

  • This function should only allow the typing of numbers and comma, as this is not happening.

  • Excuse my stupidity, but as I show this example online?

  • No, so I guess there’s no way

  • @Markim opens the browser and on the page presses Ctrl+U and copies the contents and pastes in Pastebin.com and sends the link here, so I can analyze

  • Ah, I couldn’t touch the Pastebin.com, but thanks for your attention, tomorrow I try to solve the mistake

Show 4 more comments

0

Markin,

To complement the answer, in Document.getElementById, you should use Clientid. Asp.net changes the name in some cases when it sends to the Browser.

document.getElementById("<%=TextBox1.ClientID%>");

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 assemble a mask, wouldn’t it be better to use some ready mask ? Example:
Jquery Mask Plugin
Jquery Moneymask

Browser other questions tagged

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