I'm trying to manage my if statements into a switch statement to evaluate regex, but I don't know how I can go about it. I tried many possibilities but cannot do it. See code below, is it right?:
var username = $('input.username'),
phone = $('input.phone'),
numRegex = /^[0-9]/i,
alphaRegex = /^[a-zA-Z]/i,
usernameVal = username.val(),
phoneVal = phone.val();
switch (phoneVal) {
case numRegex.test(phoneVal):
console.log('Only digits please.');
break;
default:
console.log('It\'s all good.');
}
Many thanks.
switch (false) {trueorfalse, not to compare their return value (trueorfalse) withphoneVal. This is not the way to useswitch. It seems you need a chain ofifs (anif, lots ofelse ifs, and anelse).