tags:

views:

104

answers:

2

Is this code possible?

switch (rule)
{
   case 'email' || 'valid_email':
    valid = this.validate_email(field);
    break;
}
+15  A: 

No, it is not possible, Switch statements doesn't do arithmetic calculus.

However, you can use case chaining or a bunch of if's:

switch (rule)
{
   case 'email':
   case 'valid_email':
    valid = this.validate_email(field);
    break;
}
Jhonny D. Cano -Leftware-
if chasing brevity, this one-liner would work too: case 'email': case 'valid_email':
Oren Trutner
+8  A: 

Close, but this will work:

switch (rule)
{
  case 'email':
  case 'valid_email':
    valid = this.validate_email(field);
    break;
}

The reason why it works is that without a break;, execution continues within the switch block.

Andrew Moore