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?

James Z
  • 12,209
  • 10
  • 24
  • 44
ace
  • 225
  • 4
  • 18

1 Answers1

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

Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
  • I want to implement it with ASP textbox, it's already working with the normal html input – ace Jul 10 '17 at 10:18
  • 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 only – ace Jul 10 '17 at 10:19
  • 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 me – Chandan Kumar Jul 10 '17 at 10:30
  • yeah, gone through the code, it was an issue with div mismatch. Now it's working fine with both the textboxes – ace Jul 10 '17 at 10:38