Can anyone tell me how can I check this by Regex form validation in javascript.
Student Name must contain only alphabets. There must be equal number of Vowels and Consonants.
Can anyone tell me how can I check this by Regex form validation in javascript.
Student Name must contain only alphabets. There must be equal number of Vowels and Consonants.
I don't think you can do it using Regex only.
My solution replaces all characters from a string that are not a vowel and returns it's length, same for consonants. Then all you have to do is check if the number of consonants matches the vowels.
const getNumberOfVowels = (string) => string.replace(/[^aeiouAEIOU]/g, "").length;
const getNumberOfConsonants = (string) => string.replace(/[aeiouAEIOU]/g, "").length;
const isAValidName = (name) => {
const vowels = getNumberOfVowels(name);
const consonants = getNumberOfConsonants(name);
return vowels === consonants;
}
console.log(isAValidName("Adam"));
You will need to create a function that does two things:
You will need the following expresisons:
/^[a-z]+$/i
– Only alpha characters (case-insensitive)/[aeiouy]/ig
– Only vowels/[^aeiouy]/ig
– Only consonantsconst isValidName = (name) =>
/^[a-z]+$/i.test(name) &&
name.match(/[aeiouy]/ig).length === name.match(/[^aeiouy]/ig).length;
console.log(isValidName('Gene')); // Valid
console.log(isValidName('Abby')); // Valid
console.log(isValidName('Charles')); // Invalid
The following code can be used to validate: