javascript - Count the vowels of an input? -
hi trying count vowels of input of form in html using javascript no success.
here html
<!doctype html> <html> <head> <script src='p3-vowels.js'></script> </head> <body> <form action="demo_form.asp"> phrase: <input type="text" id = "input1" name="countvowels" value="put phrase here"><br> <input type="button" id = "btn1" value="check vowels"> </form> </body> </html> and here javascript
function vow(form){ var = form.countvowels.value; flag = 0; (var = 0; < a.length; i++){ switch (a[i]){ case 'a' case 'e' case 'i' case 'o' case 'u' flag++; break; } } alert(flag); } function init(){ var button1 = document.getelementbyid("btn1") button1.onclick = vow; } window.onload = init;
another option other answers use regular expression. not easier understand, or particularly efficient, i'm giving alternative. (it continues prove proverb: give 100 programmers problem solve , you'll 100 solutions)
the following expression used in javascript match vowels, , can use returning array see how many there are...
/[aeiou]/ig the i makes case insensitive, , g means global pick multiple times.
here in action...
function vow(form){ var = form.countvowels.value; var matches = a.match(/[aeiou]/ig); var flag = 0; if (matches != null) flag = matches.length; alert(flag); } function init(){ var button1 = document.getelementbyid("btn1") button1.onclick = function() { vow(document.getelementbyid("myform")); } } window.onload = init; <form id="myform" action="demo_form.asp"> phrase: <input type="text" id = "input1" name="countvowels" value="put phrase here"><br> <input type="button" id = "btn1" value="check vowels"> </form>
Comments
Post a Comment