How To Make Counter For Numbers, Vowels, And Consonants Using JavaScript

This program that I make using JavaScript Programming Language is How To Make Counter For Numbers, Vowels, And Consonants Using JavaScript. This program will ask the user to type their sentence on the Textarea of Form Field then the program will calculate the number of vowels, consonants, and numbers. This program is very simple and you can create after downloading the source code below as your own. Hope you find this useful. HTML Source Code This code contains Textarea for the user to type their sentence.
  1. <div class="wrapper">
  2.  
  3. <table cellspacing="5" cellpadding="5" width="100%" style="border:red 1px solid;">
  4.  
  5. <tr>
  6. <td>Type your Sentence</td>
  7. <td><textarea id='sentence_counter' class="txtBox_info" autofocus/></textarea></td>
  8. </tr>
  9.  
  10. <tr>
  11. <td></td>
  12. <td></td>
  13. </tr>
  14.  
  15. <tr>
  16. <td>No. of Consonants</td>
  17. <td><input type='text' readonly="readonly" id='all_consonants' class="txtBox_readOnly" /></td>
  18. </tr>
  19.  
  20. <tr>
  21. <td>List of Consonant(s)</td>
  22. <td><input type='text' readonly="readonly" id='consonants_lists' class="txtBox_readOnly" /></td>
  23. </tr>
  24.  
  25. <tr>
  26. <td>No. of Vowels</td>
  27. <td><input type='text' readonly="readonly" id='all_vowels' class="txtBox_readOnly" /></td>
  28. </tr>
  29.  
  30. <tr>
  31. <td>List of Vowel(s)</td>
  32. <td><input type='text' readonly="readonly" id='vowels' class="txtBox_readOnly" /></td>
  33. </tr>
  34.  
  35. <tr>
  36. <td>No. of Digits</td>
  37. <td><input type='text' readonly="readonly" id='no_digits' class="txtBox_readOnly" /></td>
  38. </tr>
  39.  
  40. <tr>
  41. <td>List of Digit(s)</td>
  42. <td><input type='text' readonly="readonly" id='digits_list' class="txtBox_readOnly" /></td>
  43. </tr>
  44.  
  45. <tr>
  46. <td colspan="2">
  47. <input type='button' value='Enter' class="btn_enter" title="Click here to process." onclick="javascript:all_count();" />
  48. <input type='button' value='Clear' class="btn_cancel" title="Click here to clear all the text fields." onclick="javascript:reset_all();" />
  49. </td>
  50. </tr>
  51.  
  52. </div>
JavaScript Source Code This script will help us to calculate the number of vowels, consonants, and numbers.
  1. <script type="text/javascript">
  2. function all_count() {
  3. var str = document.getElementById('sentence_counter').value;
  4. var count = 0, total_vowels="";
  5. var count2=0, consonants_lists="";
  6. var count3=0, total_digits="";
  7. for (var i = 0; i < str.length; i++) {
  8. if (str.charAt(i).match(/[a-zA-Z]/) != null) {
  9. if (str.charAt(i).match(/[aeiouAEIOU]/))
  10. {
  11. total_vowels = total_vowels + str.charAt(i);
  12. count++;
  13. }
  14. if (str.charAt(i).match(/[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]/))
  15. {
  16. consonants_lists = consonants_lists + str.charAt(i);
  17. count2++;
  18. }
  19. }
  20. function retnum(str1) {
  21. var num = str1.replace(/[^0-9]/g, '');
  22. return num;
  23. }
  24. function count_digits(str2) {
  25. var num2 = str2.replace(/[^0-9]/g,"").length;
  26. return num2;
  27. }
  28. }
  29. document.getElementById('all_consonants').value = count2;
  30. document.getElementById('consonants_lists').value = consonants_lists;
  31. document.getElementById('vowels').value = total_vowels;
  32. document.getElementById('all_vowels').value = count;
  33. document.getElementById('no_digits').value = count_digits(str);
  34. document.getElementById('digits_list').value = retnum(str);
  35. }
  36. function reset_all()
  37. {
  38. document.getElementById('all_consonants').value ="";
  39. document.getElementById('consonants_lists').value ="";
  40. document.getElementById('vowels').value = "";
  41. document.getElementById('all_vowels').value = "";
  42. document.getElementById('no_digits').value ="";
  43. document.getElementById('digits_list').value ="";
  44. document.getElementById('sentence_counter').value ="";
  45. document.getElementById('sentence_counter').focus();
  46. }
  47. </script>
And, the style is.
  1. <style type="text/css">
  2. body {
  3. width:700px;
  4. margin:auto;
  5. }
  6. .wrapper{
  7. border: blue 1px solid;
  8. padding: 10px;
  9. margin: 25px;
  10. }
  11. td {
  12. font-size: 18px;
  13. color: blue;
  14. text-align: center;
  15. font-weight: bold;
  16. font-family: cursive;
  17. }
  18. .btn_enter {
  19. width: 100px;
  20. font-size: 18px;
  21. color: blue;
  22. background: azure;
  23. border: blue 1px solid;
  24. border-radius: 4px;
  25. padding: 5px;
  26. cursor:pointer;
  27. margin-right:20px;
  28. }
  29. .btn_cancel {
  30. width: 100px;
  31. font-size: 18px;
  32. color: red;
  33. background: azure;
  34. border: red 1px solid;
  35. border-radius: 4px;
  36. padding: 5px;
  37. cursor:pointer;
  38. }
  39. .txtBox_info {
  40. border: blue 1px solid;
  41. font-size: 18px;
  42. text-indent: 10px;
  43. background: azure;
  44. cursor: pointer;
  45. }
  46. .txtBox_readOnly {
  47. border: blue 1px solid;
  48. font-size: 18px;
  49. text-indent: 10px;
  50. text-align:center;
  51. color:red;
  52. background: #d0d0d0;
  53. cursor: no-drop;
  54. }
  55. </style>
This is the Output: Result Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment