number to words (Indian numbering system) using javascript
Asked Answered
B

1

1

REFERENCE : https://jsfiddle.net/lesson8/5tt7d3e6/ I want to use ASP textbox control to implement number to words functionality! I have javascript function which converts numbers into words onkeyup function.

My javascript function:

function convertNumberToWords(amount) {
        var words = new Array();
        words[0] = '';
        words[1] = 'One';
        words[2] = 'Two';
        words[3] = 'Three';
        words[4] = 'Four';
        words[5] = 'Five';
        words[6] = 'Six';
        words[7] = 'Seven';
        words[8] = 'Eight';
        words[9] = 'Nine';
        words[10] = 'Ten';
        words[11] = 'Eleven';
        words[12] = 'Twelve';
        words[13] = 'Thirteen';
        words[14] = 'Fourteen';
        words[15] = 'Fifteen';
        words[16] = 'Sixteen';
        words[17] = 'Seventeen';
        words[18] = 'Eighteen';
        words[19] = 'Nineteen';
        words[20] = 'Twenty';
        words[30] = 'Thirty';
        words[40] = 'Forty';
        words[50] = 'Fifty';
        words[60] = 'Sixty';
        words[70] = 'Seventy';
        words[80] = 'Eighty';
        words[90] = 'Ninety';
        amount = amount.toString();
        var atemp = amount.split(".");
        var number = atemp[0].split(",").join("");
        var n_length = number.length;
        var words_string = "";
        if (n_length <= 9) {
            var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
            var received_n_array = new Array();
            for (var i = 0; i < n_length; i++) {
                received_n_array[i] = number.substr(i, 1);
            }
            for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
                n_array[i] = received_n_array[j];
            }
            for (var i = 0, j = 1; i < 9; i++, j++) {
                if (i == 0 || i == 2 || i == 4 || i == 7) {
                    if (n_array[i] == 1) {
                        n_array[j] = 10 + parseInt(n_array[j]);
                        n_array[i] = 0;
                    }
                }
            }
            value = "";
            for (var i = 0; i < 9; i++) {
                if (i == 0 || i == 2 || i == 4 || i == 7) {
                    value = n_array[i] * 10;
                } else {
                    value = n_array[i];
                }
                if (value != 0) {
                    words_string += words[value] + " ";
                }
                if ((i == 1 && value != 0) || (i == 0 && value != 0 && n_array[i + 1] == 0)) {
                    words_string += "Crores ";
                }
                if ((i == 3 && value != 0) || (i == 2 && value != 0 && n_array[i + 1] == 0)) {
                    words_string += "Lakhs ";
                }
                if ((i == 5 && value != 0) || (i == 4 && value != 0 && n_array[i + 1] == 0)) {
                    words_string += "Thousand ";
                }
                if (i == 6 && value != 0 && (n_array[i + 1] != 0 && n_array[i + 2] != 0)) {
                    words_string += "Hundred and ";
                } else if (i == 6 && value != 0) {
                    words_string += "Hundred ";
                }
            }
            words_string = words_string.split("  ").join(" ");
        }
        return words_string;
    }

Code for ASP textbox:

 <asp:TextBox ID="txtBudget" runat="server" class="form-control input-sm" placeholder="" TabIndex="1" onkeyup="wordDiv.innerHTML=convertNumberToWords(this.value);" onkeypress="return keyRestrictValidChars(event, '1234567890,');"></asp:TextBox>
 <div id="wordDiv"></div>

But i am unable to figure out why it's not working.

if i replace ASP textbox with following code:

<input type="text" name="number" placeholder="Number OR Amount" onkeyup="wordDiv.innerHTML=convertNumberToWords(this.value)" />
 <div id="wordDiv"></div>

Then it works fine!

While using asp textbox i tried following too:

onkeyup="return convertNumberToWords(this.value);"

and

onkeyup="return convertNumberToWords(this.value, 'wordDiv');"

But neither worked!

can anyone figure out what is the issue with asp textbox while using same function?

Benzedrine answered 10/7, 2017 at 10:12 Comment(8)
Tested your shared code on a separate web application. It working fine with me.Lelalelah
@Prabhat it's working fine with normal input type, i wanted to make it workable with ASP textboxBenzedrine
Yes...I have used ASP Textbox server control not HTML input.Lelalelah
@Prabhat can you please share demo?Benzedrine
I am saying same in my web application I have used ASP server control and it's working perfectly there is no issue in your shared code. Try to look at browser developer console and see if there is any javascript error which is disrupting your javascript calls.Lelalelah
@Prabhat ohk i ll just check that outBenzedrine
Surely, I could have but there isn't any asp.net online compiler available on web to share a demo.Lelalelah
yeah, gone through the code, it was an issue with div mismatch. Now it's working fine with both the textboxesBenzedrine
T
1

I tried both of them and it is working as expected.

Below is the one which contains both implementation along with the output

Default.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebFormApplication1._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        function convertNumberToWords(amount) {
            var words = new Array();
            words[0] = '';
            words[1] = 'One';
            words[2] = 'Two';
            words[3] = 'Three';
            words[4] = 'Four';
            words[5] = 'Five';
            words[6] = 'Six';
            words[7] = 'Seven';
            words[8] = 'Eight';
            words[9] = 'Nine';
            words[10] = 'Ten';
            words[11] = 'Eleven';
            words[12] = 'Twelve';
            words[13] = 'Thirteen';
            words[14] = 'Fourteen';
            words[15] = 'Fifteen';
            words[16] = 'Sixteen';
            words[17] = 'Seventeen';
            words[18] = 'Eighteen';
            words[19] = 'Nineteen';
            words[20] = 'Twenty';
            words[30] = 'Thirty';
            words[40] = 'Forty';
            words[50] = 'Fifty';
            words[60] = 'Sixty';
            words[70] = 'Seventy';
            words[80] = 'Eighty';
            words[90] = 'Ninety';
            amount = amount.toString();
            var atemp = amount.split(".");
            var number = atemp[0].split(",").join("");
            var n_length = number.length;
            var words_string = "";
            if (n_length <= 9) {
                var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
                var received_n_array = new Array();
                for (var i = 0; i < n_length; i++) {
                    received_n_array[i] = number.substr(i, 1);
                }
                for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
                    n_array[i] = received_n_array[j];
                }
                for (var i = 0, j = 1; i < 9; i++, j++) {
                    if (i == 0 || i == 2 || i == 4 || i == 7) {
                        if (n_array[i] == 1) {
                            n_array[j] = 10 + parseInt(n_array[j]);
                            n_array[i] = 0;
                        }
                    }
                }
                value = "";
                for (var i = 0; i < 9; i++) {
                    if (i == 0 || i == 2 || i == 4 || i == 7) {
                        value = n_array[i] * 10;
                    } else {
                        value = n_array[i];
                    }
                    if (value != 0) {
                        words_string += words[value] + " ";
                    }
                    if ((i == 1 && value != 0) || (i == 0 && value != 0 && n_array[i + 1] == 0)) {
                        words_string += "Crores ";
                    }
                    if ((i == 3 && value != 0) || (i == 2 && value != 0 && n_array[i + 1] == 0)) {
                        words_string += "Lakhs ";
                    }
                    if ((i == 5 && value != 0) || (i == 4 && value != 0 && n_array[i + 1] == 0)) {
                        words_string += "Thousand ";
                    }
                    if (i == 6 && value != 0 && (n_array[i + 1] != 0 && n_array[i + 2] != 0)) {
                        words_string += "Hundred and ";
                    } else if (i == 6 && value != 0) {
                        words_string += "Hundred ";
                    }
                }
                words_string = words_string.split("  ").join(" ");
            }
            return words_string;
        }
    </script>

    <div class="row">
        <div class="col-md-4">
            Asp.net textbox : 
             <asp:TextBox ID="txtBudget" runat="server" class="form-control input-sm" placeholder="" TabIndex="1" 
                 onkeyup="wordDiv1.innerHTML=convertNumberToWords(this.value);" 
                 onkeypress="return keyRestrictValidChars(this.value);"></asp:TextBox>
            <div id="wordDiv1"></div>
        </div>
        <br />
    </div>
     <div class="row">
        <div class="col-md-4">
            Html Textbox : 
             <input type="text" name="number" placeholder="Number OR Amount" onkeyup="wordDiv2.innerHTML=convertNumberToWords(this.value)" />
            <div id="wordDiv2"></div>
        </div>

    </div>

</asp:Content>

Output

enter image description here

Teryl answered 10/7, 2017 at 10:16 Comment(4)
I want to implement it with ASP textbox, it's already working with the normal html inputBenzedrine
as some other javascript function is also being applied on the same textbox, so i can't use normal input type, i wanted to do it using ASP textbox onlyBenzedrine
it seems some other thing making you in problem. I tried with both asp.net textbox and html textbox as well. both the scenarios works for meTeryl
yeah, gone through the code, it was an issue with div mismatch. Now it's working fine with both the textboxesBenzedrine

© 2022 - 2024 — McMap. All rights reserved.