Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Guys,, I'm trying to make format currency in Indonesian using javascript but why the textbox not firing when keyboard pressed.if this code running in html working properly, but not in ASP.NET. Anny help could be appriciate.


Currency Format in Javascript


What I have tried:

Currency Format in Javascript
<asp:textbox id="txtCurrency" runat="server" onkeyup="FormatRupiah(); return false;">
<pre>">
    
        $(document).ready(function () {
            function FormatRupiah() {
                var input = document.getElementById("<%=txtCurrency.ClientID%>");
                input.addEventListener('keyup', function(e)
                {
                    var number_string = bilangan.replace(/[^,\d]/g, '').toString(),
                    split = number_string.split(','),
                    sisa = split[0].length % 3,
                    rupiah = split[0].substr(0, sisa),
                    ribuan = split[0].substr(sisa).match(/\d{1,3}/gi);

                    if (ribuan) {
                        separator = sisa ? '.' : '';
                        rupiah += separator + ribuan.join('.');
                    }
                    rupiah = split[1] != undefined ? rupiah + ',' + split[1] : rupiah;
                    return prefix == undefined ? rupiah : (rupiah ? 'Rp. ' + rupiah : '');
                });
            }
        });
    

Code Bihind
protected void Page_Load(object sender, EventArgs e)
    {
        txtCurrency.Attributes.Add("onkeyup", "FormatRupiah(this)");
    }
<pre>
Posted
Updated 15-Jun-21 19:12pm

Your code is extremely confused. You have the onkeyup attribute set twice, which calls the FormatRupiah function when the keyup event is fired.

That function then attaches an anonymous event handler to the keyup event, which will be called the next time the keyup event is fired.

Each subsequent keyup event will add yet another handler calling the anonymous function. Eventually your browser will grind to a halt, calling the same function thousands of times for every key press.

Your anonymous function also refers to variables which have not be declared anywhere, so you will get an error - for example, bilangan.

Attach the actual event handler once. Make sure all of your variables are declared and initialized properly. If it still doesn't work, then you'll need to provide the actual details of the error.
 
Share this answer
 
Comments
tri setia 15-Jun-21 23:23pm    
@Richard Deeming
thanks for the explaining sir, but l'm was test that code in html running properly and then l'm change that code in asp.net just change a little but not working.. please correct my code and how it can be running.
problem solved
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Money.aspx.cs" Inherits="Money" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="<%=Page.ResolveClientUrl("~/Assets/plugins/jquery/jquery.min.js") %>"></script>
    <link rel="stylesheet" type="text/css" href="<%=Page.ResolveClientUrl("~/Assets/dist/css/adminlte.min.css") %>" />
    <script type="text/javascript" lang="javascript">
        window.onload = function () {
            var input = document.getElementById("<%=txtCurrency.ClientID%>");
            input.addEventListener('keyup', function (e) {
                input.value = FormatRupiah(this.value, 'Rp. ');
            });
            function FormatRupiah(bilangan, prefix) {
                var number_string = bilangan.replace(/[^,\d]/g, '').toString(),
                    split = number_string.split(','),
                    sisa = split[0].length % 3,
                    rupiah = split[0].substr(0, sisa),
                    ribuan = split[0].substr(sisa).match(/\d{1,3}/gi);
                if (ribuan) {
                    separator = sisa ? '.' : '';
                    rupiah += separator + ribuan.join('.');
                }

                rupiah = split[1] != undefined ? rupiah + ',' + split[1] : rupiah;
                return prefix == undefined ? rupiah : (rupiah ? 'Rp. ' + rupiah : '');
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div class="container mt-4">
            <div align="center">
                <asp:TextBox ID="txtCurrency" CssClass="form-control" runat="server"></asp:TextBox>
            </div>
        </div>
    </form>
</body>
</html>
C#

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900