|
| 1 | +var input = document.querySelector("#word-count-input"); |
| 2 | +var total_word = document.querySelector("#word-count"); |
| 3 | +var total_character = document.querySelector("#character-count"); |
| 4 | +var alphabets = document.querySelector('#alphabets'); |
| 5 | +var white_space = document.querySelector('#white-space'); |
| 6 | +var number = document.querySelector('#numbers'); |
| 7 | +var special_character = document.querySelector('#special-character'); |
| 8 | +window.addEventListener( |
| 9 | + "input", |
| 10 | + function (event) { |
| 11 | + if (event.target.matches("#word-count-input")) { |
| 12 | + count(); |
| 13 | + } |
| 14 | + }, |
| 15 | + false |
| 16 | +); |
| 17 | +const reset = () => { |
| 18 | + document.getElementById("word-count").innerHTML="0"; |
| 19 | + document.getElementById("character-count").innerHTML="0"; |
| 20 | + document.getElementById("alphabets").innerHTML="0"; |
| 21 | + document.getElementById("white-space").innerHTML="0"; |
| 22 | + document.getElementById("numbers").innerHTML="0"; |
| 23 | + document.getElementById("special-character").innerHTML="0"; |
| 24 | + var textbox=document.getElementById("word-count-input"); |
| 25 | + textbox.value=""; |
| 26 | +} |
| 27 | +function count(){ |
| 28 | + |
| 29 | + let text = input.value; |
| 30 | + |
| 31 | + //Count Total words |
| 32 | + var TotalWords = text.split(/[\n\r\s]+/g).filter(function (word) { |
| 33 | + return word.length > 0; |
| 34 | + }); |
| 35 | + total_word.innerHTML = TotalWords.length; |
| 36 | + |
| 37 | + //Count Total Characters |
| 38 | + let TotalCharacter = text.length; |
| 39 | + total_character.innerHTML = TotalCharacter; |
| 40 | + |
| 41 | + //Count Alphabets |
| 42 | + // here /g means Global, and causes the replace call to replace all matches(every occurence), |
| 43 | + let text_Without_Whitespace = text.replace(/\s+/g, ""); // here \s matches with each whitespace replace with no space(remove it) |
| 44 | + let Alphabets_Without_Numbers = text_Without_Whitespace.replace(/[0-9]/g, ""); |
| 45 | + let Alphabets_Without_Punctuation = Alphabets_Without_Numbers.replace(/[|\\+@<>'"?.,\/#!$%\^&\*;:{}\[\]=\-_`~()]/g, ""); |
| 46 | + |
| 47 | + alphabets.innerHTML = Alphabets_Without_Punctuation.length; |
| 48 | + |
| 49 | + //Count Numbers |
| 50 | + let Number = text.replace(/[^\d]/g, ''); // here /d matches any single number - here replace all characters except number[^] |
| 51 | + number.innerHTML = Number.length; |
| 52 | + |
| 53 | + //Count White-Space |
| 54 | + let WhiteSpace = text.replace(/[a-zA-Z0-9|\\+@<>'"?.,\/#!$%\^&\*;:{}\[\]=\-_`~()]/g, ''); |
| 55 | + white_space.innerHTML = WhiteSpace.length; |
| 56 | + |
| 57 | + //Count Special Characters |
| 58 | + let SpecialCharacter = text_Without_Whitespace.replace(/[a-zA-Z0-9]/g, ''); |
| 59 | + special_character.innerHTML = SpecialCharacter.length; |
| 60 | + |
| 61 | + // Changing Colours |
| 62 | + if(TotalWords.length > 0){ |
| 63 | + total_word.style.color = 'red'; |
| 64 | + } |
| 65 | + else{ |
| 66 | + total_word.style.color = 'black'; |
| 67 | + } |
| 68 | + |
| 69 | + if(TotalCharacter > 0){ |
| 70 | + total_character.style.color = 'red'; |
| 71 | + } |
| 72 | + else{ |
| 73 | + total_character.style.color = 'black'; |
| 74 | + } |
| 75 | + |
| 76 | + if(Alphabets_Without_Punctuation.length > 0){ |
| 77 | + alphabets.style.color = 'red'; |
| 78 | + } |
| 79 | + else{ |
| 80 | + alphabets.style.color = 'black'; |
| 81 | + } |
| 82 | + |
| 83 | + if(Number.length > 0){ |
| 84 | + number.style.color = 'red'; |
| 85 | + } |
| 86 | + else{ |
| 87 | + number.style.color = 'black'; |
| 88 | + } |
| 89 | + |
| 90 | + if(WhiteSpace.length > 0){ |
| 91 | + white_space.style.color = 'red'; |
| 92 | + } |
| 93 | + else{ |
| 94 | + white_space.style.color = 'black'; |
| 95 | + } |
| 96 | + |
| 97 | + if(SpecialCharacter.length > 0){ |
| 98 | + special_character.style.color = 'red'; |
| 99 | + } |
| 100 | + else{ |
| 101 | + special_character.style.color = 'black'; |
| 102 | + } |
| 103 | +} |
0 commit comments