1

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.

8
  • So long as those vars are in scope just switch (false) { Commented Oct 25, 2011 at 17:52
  • I believe switch only allows for testing one condition Commented Oct 25, 2011 at 17:53
  • Why you are tying to build your own validation scheme while you can easly be accomplished by something like jQuery validation Commented Oct 25, 2011 at 17:55
  • @Emmanuel N: Because that's what my company wants. Commented Oct 25, 2011 at 17:56
  • 1
    But you want to check whether the tests return true or false, not to compare their return value (true or false) with phoneVal. This is not the way to use switch. It seems you need a chain of ifs (an if, lots of else ifs, and an else). Commented Oct 25, 2011 at 18:31

3 Answers 3

4

I think this kind of defeats the point of the switch statement having the conditions in the case statements. It's intent is to test multiple outputs given a single input.

Perhaps if JavaScript supported multiple inputs (or destructuring of arrays) in a switch..case I could see something like this:

switch (numRegex.test(phoneVal), alphaRegex.test(usernameVal)) {
    case false, false:
        console.log('Neither are valid.');
        break;

    case true, false:
        console.log('Invalid username.');
        break;

    /* snip */

    default:
        console.log('All\'s good.');
}

But alas, I'd say sticking with if..else is a better and easier to follow/understand option, in this case:

if (numRegex.test(phoneVal)) {
    console.log('Only digits please.');
    return false;
} else if (alphaRegex.test(usernameVal)) {
    console.log('Only alpha-numeric please.');
    return false;
} else {
    console.log('It\'s all good.');
    return true;
}
Sign up to request clarification or add additional context in comments.

1 Comment

But some language do accept multiple expression in the switch, but not javascript...!!? Guess I have to use if's all over the place. Anyways thanks for your answer.
0

If you are only using each regex once why not just refactor this into a block of ternaries?

Maybe something like this:

/^\S/.test($('#name').val()) !== true
? invalidEls.push('name:invalid')
: invalidEls.push('name:valid');

$('#phone').val().match(/^(1-?)?([2-9]\d{2}|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/)
? invalidEls.push('phone:valid')
: invalidEls.push('phone:invalid');

$('#email').val().match(/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9]){1,}?/)
? invalidEls.push('email:valid')
: invalidEls.push('email:invalid');

Comments

0

Another similar pattern;

try {
    if (!alphaRegex.test(usernameVal))
        throw ('Only letters please.');

    if (!numRegex.test(phoneVal))
         throw ('Only digits please.');

    ...
    return true;
} catch (e) {
    console.log(e);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.