Last active
April 22, 2026 10:42
-
-
Save milan-sahana/e66042df639ace914254ee28bc35c05d to your computer and use it in GitHub Desktop.
numberToWordsIndian javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function numberToWordsIndian(num) { | |
| const ones = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ', | |
| 'ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', | |
| 'seventeen ', 'eighteen ', 'nineteen ']; | |
| const tens = ['', '', 'twenty ', 'thirty ', 'forty ', 'fifty ', 'sixty ', 'seventy ', 'eighty ', 'ninety ']; | |
| if (num === 0) return 'zero'; | |
| let str = ''; | |
| let crore = Math.floor(num / 10000000); | |
| num %= 10000000; | |
| let lakh = Math.floor(num / 100000); | |
| num %= 100000; | |
| let thousand = Math.floor(num / 1000); | |
| num %= 1000; | |
| let hundred = Math.floor(num / 100); | |
| let rest = num % 100; | |
| if (crore) str += numberToWordsIndian(crore) + 'crore '; | |
| if (lakh) str += numberToWordsIndian(lakh) + 'lakh '; | |
| if (thousand) str += numberToWordsIndian(thousand) + 'thousand '; | |
| if (hundred) str += ones[hundred] + 'hundred '; | |
| if (rest) { | |
| if (rest < 20) { | |
| str += ones[rest]; | |
| } else { | |
| str += tens[Math.floor(rest / 10)] + ones[rest % 10]; | |
| } | |
| } | |
| return str.trim(); | |
| } | |
| <!-- | |
| $(document).ready(function() { | |
| function validateAmount() { | |
| var min = $('#pack_id option:selected').data('min'); | |
| var rawValue = $('#amount').val(); | |
| // Remove anything except digits | |
| var cleanValue = rawValue.replace(/[^0-9]/g, ''); | |
| var amount = Number(cleanValue); | |
| if (!cleanValue || isNaN(amount)) { | |
| $('#numberToWord').html(''); | |
| return false; | |
| } | |
| // Convert to words safely | |
| let words = numberToWordsIndian(amount); | |
| words = words.charAt(0).toUpperCase() + words.slice(1) + ' only'; | |
| console.log(min); | |
| // Validation | |
| if (min && amount < min) { | |
| $('#numberToWord').html( | |
| "<span style='color:red;'>Minimum amount for this plan is ₹" + min + "</span><br>" + | |
| "<span>" + words + "</span>" | |
| ); | |
| return false; | |
| } else { | |
| $('#numberToWord').html("<span>" + words + "</span>"); | |
| return true; | |
| } | |
| } | |
| $('#pack_id').change(validateAmount); | |
| $('#amount').on('input', validateAmount); | |
| $("#frmAdd").submit(function(event) { | |
| if (!confirm('Are you sure that you want to submit the form')) | |
| event.preventDefault(); | |
| }); | |
| }); | |
| --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment