Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>[<b>edit</b>] Let's add this disclaimer: this use of <code>switch</code> is considered EVIL or ABUSIVE. Some Disciples Of The Javascript also consider the use of <em>ternaries</em> as a major SIN. So beware of using them, because <a href="http://www.reddit.com/r/javascript/comments/eoq10/the_javascript_cockroach_from_hell/" rel="nofollow">hell</a> may await you, so the Gospel goes.</p> <p>You can use <code>switch</code> like this <sup><b>1</b></sup>:</p> <pre><code>switch (true) { case (parseInt(charCode) &gt;= 65 &amp;&amp; parseInt(charCode) &lt;=90): //UPPERCASE alert("UP"); break; case (parseInt(charCode) &gt;= 97 &amp;&amp; parseInt(charCode) &lt;=122): //LOWERCASE alert("LO"); break; case (parseInt(charCode) &gt;= 48 &amp;&amp; parseInt(charCode) &lt;=57): //NNUMBERS alert("NUM"); break; default: break } </code></pre> <p>it's pretty bulky. You don't have to use <code>parseInt</code> if you derive <code>charCode</code> came from <code>event.keyCode</code>. If you need to use <code>parseInt</code>, don't forget to provide the <a href="http://www.devguru.com/technologies/ecmascript/quickref/parseint.html" rel="nofollow">radix</a>, or better still, use <code>Number</code> to convert to a number.</p> <p>Anyway using a <a href="http://en.wikipedia.org/wiki/Ternary" rel="nofollow">ternary</a> is an alternative for this:</p> <pre><code>alert( charCode &gt;= 65 &amp;&amp; charCode &lt;= 90 ? 'UP' : charCode &gt;= 97 &amp;&amp; charCode &lt;= 122 ? 'LO' : charCode &gt;= 48 &amp;&amp; charCode &lt;= 57 ? 'NUM' : 'OTHER' ); </code></pre> <p>[<strong><em>edit</em></strong>] Let's see if the following alternative can satisfy the church of Javascript Coders...</p> <p>Another alternative is using a <code>RegExp</code> with the character derived from the <code>keyCode</code>:</p> <pre><code>var chr = String.fromCharCode(charCode), alertval = /[a-z]/.test(chr) ? 'LO' : /[A-Z]/.test(chr) ? 'UP' : /[0-9]/.test(chr) ? 'NUM' : 'OTHER'; alert(alertval); </code></pre> <p>As a last alternative (man, javascript is <em>so</em> versatile!) I present:</p> <pre><code>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'; }); </code></pre> <p><sup><b>1</b></sup> <a href="http://getmockery.com/blog/archives/80" rel="nofollow">some notes</a> on that 'trick'</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload