Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to perform a case insensitive string comparison?
    primarykey
    data
    text
    <p>I have a textbox on my website and the user can input commands. But the thing is the commands are case sensitive. How do I make the commands not case sensitive? Here is my code: </p> <p>JavaScript:</p> <pre><code>function showAlert() { var txtCtrl = document.getElementById("textbox1"); var txtVal = txtCtrl.value; if (txtVal == '') { alert('Please fill in the text box. For a list of commands type "Help" into the text box.'); }else if (txtVal == 'Start' || txtVal == 'start') { alert('Hello. What would you like me to do?'); }else if (txtVal === 'Weather' || txtVal === 'weather') { window.location = "https://www.google.com/#q=weather"; }else if (txtVal === 'Time' || txtVal === 'time') { alert('The current time according to your computer is' + formatTime(new Date())); }else if (txtVal === 'Help' || txtVal === 'help') { window.location = "help/index.html"; }else if (txtVal === 'Donate' || txtVal === 'donate') { window.location = "donate/index.html"; }else if (txtVal === 'www.' || txtVal === 'WWW.') { }else{ alert('Sorry, I do not reconise that command. For a list of commands, type "Help" into the text box.'); } } //Show time in 24hour format function showTime(){ var today = new Date(); var h = today.getHours(); var m = today.getMinutes(); return [ h, m ].join(':') } //Show time in 12hour format var formatTime = (function () { function addZero(num) { return (num &gt;= 0 &amp;&amp; num &lt; 10) ? "0" + num : num + ""; } return function (dt) { var formatted = ''; if (dt) { var hours24 = dt.getHours(); var hours = ((hours24 + 11) % 12) + 1; formatted = [formatted, [addZero(hours), addZero(dt.getMinutes())].join(":"), hours24 &gt; 11 ? "PM" : "AM"].join(" "); } return formatted; } })(); </code></pre> <p>And HTML:</p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Random Project&lt;/title&gt; &lt;/head&gt; &lt;style type="text/css"&gt; body { margin:0; font-family: arial, sans-serif; padding-top: 80px; } .container { margin:auto; text-align:center; } .logo { margin:auto; width:512px; text-align:center; padding-bottom:0px; } #textbox1 { border: 1px solid #ddd; border-radius: 0px 0px 0px 0px; width: 512px; padding-top: 5px; padding-bottom: 5px; padding-left: 7px; padding-right: 7px; height: 20px; font-size: 16px; outline: none; font-family: arial, sans-serif; } #textbox1:focus { border: 1px solid #0266C8; } #button1 { margin-top: 22px; padding: 2px 10px 2px 10px; outline:none; background-color:#eee; border:1px solid #ddd; border-radius:2px; } #button1:hover { background-color: #f5f5f5; } #button1:focus { border: 1px solid #0266C8; } #button1_text { font:bold 11px/27px Arial,sans-serif!important; color:#333; } .information { font-size:12px; color:#555; } .separator { height:100px; } .tip { color:green; font-weight:bold; font-size:12px; margin-top:10px; } .tip_text { color:#111; font-weight:normal; } &lt;/style&gt; &lt;body&gt; &lt;div class="container"&gt; &lt;img class="logo" src="logo.png" width="450" height="110" alt="Random Project"&gt; &lt;input type="text" name="textbox1" value="" spellcheck="false" dir="ltr" placeholder="Type here" id="textbox1"&gt;&lt;br&gt; &lt;button id="button1" name="button1" aria-label="Help me" onClick="showAlert();"&gt; &lt;span id="button1_text"&gt;Help me&lt;/span&gt; &lt;/button&gt; &lt;div class="separator"&gt;&lt;/div&gt; &lt;span class="information"&gt;&amp;copy; Copyright DemDevs 2013. All rights reserved. Made by Omar Latreche&lt;br&gt;&lt;a href="donate/index.html"&gt;Donate now&lt;/a&gt;&lt;/span&gt; &lt;div class="tip"&gt; &lt;span class="tip"&gt;Tip: &lt;/span&gt;&lt;span class="tip_text"&gt;The commands are NOT case sensitive&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class=""&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Any help will be greatly appreciated.</p> <p>Thanks, Omar!</p> <p>Edit:</p> <p>I figured it out, I made two new variables: </p> <pre><code>var txtValUpper = txtVal.toUpperCase(); </code></pre> <p>and </p> <pre><code>var txtValLower = txtVal.toLowerCase(); </code></pre> <p>then put them into the code for example: </p> <pre><code>if (txtValUpper == 'start' || txtValLower == 'start') { alert('Hello. What would you like me to do?'); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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