Number (currency) to word (currency) (Indian currency) on textbox click using JavaScript

In this article, we learn how to change number currency to word currency using JavaScript. 

Source code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="numbertoword.aspx.cs" Inherits="numbertoword" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function onlyNumbers(evt) {
            var e = event || evt;
            var charCode = e.which || e.keyCode;

            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
            return true;
        }

        function NumToWord(inputNumber, outputControl) {
            var str = new String(inputNumber)
            var splt = str.split("");
            var rev = splt.reverse();
            var once = ['Zero', ' One', ' Two', ' Three', ' Four', ' Five', ' Six', ' Seven', ' Eight', ' Nine'];
            var twos = ['Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', ' Seventeen', ' Eighteen', ' Nineteen'];
            var tens = ['', 'Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety'];

            numLength = rev.length;
            var word = new Array();
            var j = 0;

            for (i = 0; i < numLength; i++) {
                switch (i) {

                    case 0:
                        if ((rev[i] == 0) || (rev[i + 1] == 1)) {
                            word[j] = '';
                        }
                        else {
                            word[j] = '' + once[rev[i]];
                        }
                        word[j] = word[j];
                        break;

                    case 1:
                        aboveTens();
                        break;

                    case 2:
                        if (rev[i] == 0) {
                            word[j] = '';
                        }
                        else if ((rev[i - 1] == 0) || (rev[i - 2] == 0)) {
                            word[j] = once[rev[i]] + " Hundred ";
                        }
                        else {
                            word[j] = once[rev[i]] + " Hundred and";
                        }
                        break;

                    case 3:
                        if (rev[i] == 0 || rev[i + 1] == 1) {
                            word[j] = '';
                        }
                        else {
                            word[j] = once[rev[i]];
                        }
                        if ((rev[i + 1] != 0) || (rev[i] > 0)) {
                            word[j] = word[j] + " Thousand";
                        }
                        break;


                    case 4:
                        aboveTens();
                        break;

                    case 5:
                        if ((rev[i] == 0) || (rev[i + 1] == 1)) {
                            word[j] = '';
                        }
                        else {
                            word[j] = once[rev[i]];
                        }
                        if (rev[i + 1] !== '0' || rev[i] > '0') {
                            word[j] = word[j] + " Lac";
                        }

                        break;

                    case 6:
                        aboveTens();
                        break;

                    case 7:
                        if ((rev[i] == 0) || (rev[i + 1] == 1)) {
                            word[j] = '';
                        }
                        else {
                            word[j] = once[rev[i]];
                        }
                        if (rev[i + 1] !== '0' || rev[i] > '0') {
                            word[j] = word[j] + " Crore";
                        }
                        break;

                    case 8:
                        aboveTens();
                        break;



                    case 9:
                        if ((rev[i] == 0) || (rev[i + 1] == 1)) {
                            word[j] = '';
                        }
                        else {
                            word[j] = once[rev[i]];
                        }
                        if (rev[i + 1] !== '0' || rev[i] > '0') {
                            word[j] = word[j] + " Arab";
                        }
                        break;


                    case 10:
                        aboveTens();
                        break;

                    default: break;
                }
                j++;
            }

            function aboveTens() {
                if (rev[i] == 0) { word[j] = ''; }
                else if (rev[i] == 1) { word[j] = twos[rev[i - 1]]; }
                else { word[j] = tens[rev[i]]; }
            }

            word.reverse();
            var finalOutput = '';
            for (i = 0; i < numLength; i++) {
                finalOutput = finalOutput + word[i];
            }
            document.getElementById(outputControl).innerHTML = finalOutput;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <fieldset style="width:350px">
                <legend>Number Amount To Word</legend>

                <asp:TextBox ID="txtnumber" number runat="server" MaxLength="9" onkeypress="return onlyNumbers(this.value);" onkeyup="NumToWord(this.value,'lblword');"></asp:TextBox>
                <br />
                <asp:Label runat="server" ID="lblword"></asp:Label>
            </fieldset>
        </div>
    </form>
</body>
</html>


Out-put:-

Number (currency) to word (currency) (Indian currency) on textbox click using JavaScript Number (currency) to word (currency) (Indian currency) on textbox click using JavaScript Reviewed by NEERAJ SRIVASTAVA on 10:49:00 AM Rating: 5

No comments:

Powered by Blogger.