Reputation: 267177
Is this code possible?
switch (rule)
{
case 'email' || 'valid_email':
valid = this.validate_email(field);
break;
}
Upvotes: 3
Views: 240
Reputation: 95424
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.
Upvotes: 8
Reputation: 18013
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;
}
Upvotes: 15