Switch Statement in Javascript
Can anyone help me?
This doesn't seem to work:
switch (parseInt(charCode))
{
case (charCode >= 65 && charCode <=90): //UPPERCASE
alert("UP");
break;
case (charCode >= 97 && charCode <=122): //LOWERCASE
alert("LO");
break;
case (charCode >= 48 && charCode <=57): //NNUMBERS
alert("NUM");
break
开发者_如何学Python
}
Thanks
Cases must be single values, not expressions. If you need to use expressions, use an if
else if
series:
var numCharCode = parseInt(charCode, 10); // Always best to include the radix argument
if (numCharCode >= 65 && numCharCode <=90) { //UPPERCASE
alert("UP");
}
else if (numCharCode >= 97 && numCharCode <=122) { //LOWERCASE
alert("LO");
}
else if (numCharCode >= 48 && numCharCode <=57) { //NNUMBERS
alert("NUM");
}
(Always assuming charCode
really is a string in the first place. The name suggests it's already a number.)
Off-topic: Your logic is English-centric (and fails even for English, on words we've copied from other languages, like "naïve"). If that's okay, great, but if not you'll want to look more into how you reliably determine the case of a character in a language-neutral way. More in this answer here on StackOverflow.
[edit] Let's add this disclaimer: this use of switch
is considered EVIL or ABUSIVE. Some Disciples Of The Javascript also consider the use of ternaries as a major SIN. So beware of using them, because hell may await you, so the Gospel goes.
You can use switch
like this 1:
switch (true) {
case (parseInt(charCode) >= 65 && parseInt(charCode) <=90): //UPPERCASE
alert("UP");
break;
case (parseInt(charCode) >= 97 && parseInt(charCode) <=122): //LOWERCASE
alert("LO");
break;
case (parseInt(charCode) >= 48 && parseInt(charCode) <=57): //NNUMBERS
alert("NUM");
break;
default: break
}
it's pretty bulky. You don't have to use parseInt
if you derive charCode
came from event.keyCode
. If you need to use parseInt
, don't forget to provide the radix, or better still, use Number
to convert to a number.
Anyway using a ternary is an alternative for this:
alert( charCode >= 65 && charCode <= 90
? 'UP'
: charCode >= 97 && charCode <= 122
? 'LO'
: charCode >= 48 && charCode <= 57
? 'NUM'
: 'OTHER'
);
[edit] Let's see if the following alternative can satisfy the church of Javascript Coders...
Another alternative is using a RegExp
with the character derived from the keyCode
:
var chr = String.fromCharCode(charCode),
alertval = /[a-z]/.test(chr) ? 'LO'
: /[A-Z]/.test(chr) ? 'UP'
: /[0-9]/.test(chr) ? 'NUM'
: 'OTHER';
alert(alertval);
As a last alternative (man, javascript is so versatile!) I present:
var check = String.fromCharCode(charCode)
.replace(/([a-z])|([A-Z])|([0-9])|(.+)/,
function(a,b,c,d) {
return b ? 'LO' : c ? 'UP' : d ? 'NUM' : 'OTH';
});
1 some notes on that 'trick'
You shouldn't take as true the supposition about Javascript uses ASCII character table (doesn't it use UTF8?), so instead of numerical value comparison, just use character (string) value comparison. You shouldn't use switch statement in such a way: as in your example, it performs a (value and type, "===") comparison between parseInt(charCode) (which should be an integer) and boolean expression values such that returned by your case statements. Use and IF chain, as follows (supposing charCode is still a string value):
if (charCode >= 'A' && charCode <='Z') { //UPPERCASE
alert("UP");
} else if (charCode >= 'a' && charCode <='z') { //LOWERCASE
alert("LO");
break;
} else if (charCode >= '0' && charCode <='9'): //NNUMBERS
alert("NUM");
}
For ASCII numerical value of a char, use Javascript "asc" function, and so if you charcode is coming from a key event, you can use the inverse, the chr(keyCode) function in order to obtain a string (character) value of your already numerical keycode. I hope it was helpful :)
don't use switch here - it is not correct syntax and charCode is an int so no need to parseInt it
if (charCode >= 65 && charCode <=90) //UPPERCASE
alert("UP");
else if (charCode >= 97 && charCode <=122) //LOWERCASE
alert("LO");
else if (charCode >= 48 && charCode <=57) // NUMBERS
alert("NUM");
UPDATE: How about fromCharCode
var char = String.fromCharCode(charCode)
if (/[a-z]/.test(char)) alert("Lower");
else if (/[A-Z]/.test(char)) alert("Upper");
else if (/[0-9]/.test(char)) alert("Numbers");
http://jsfiddle.net/mplungjan/v65Xc/
I am sure someone can put this in an object or use prototype :)
Please visit this website for functions and explanation related to your problem. Sorry but none of them takes in consideration the problem related to non-ascii chars like 'ï' or 'ö' http://javascript.about.com/library/blvalid02.htm
精彩评论