简体   繁体   中英

How do I make case function non case sensitive?

        <script type='text/javascript'>
        function redirect() {
            var input = document.getElementById('userInput').value;
            switch(input) {
                case 'ALCOA':
                    window.location.replace('alcoa-Forms.htm');
                    break;
                case 'alcoa':
                    window.location.replace('/alcoa-Forms.htm');    
                    break;

How do I make it so this function is not case sensitive so I can just write it once?

simplest way is to make the input all upper or all lower case. Take your pick:

input = input.toUpperCase();
switch (input) {
   case 'ALCOA':
      ...

Keep in mind that this will also work for Alcoa , aLcOa , etc.

You could also just write case twice:

switch (input) {
   case 'ALCOA':
   case 'alcoa':

make the input uppercase:

<script type='text/javascript'>
    function redirect() {
        var input = document.getElementById('userInput').value;
        input = input.toUpperCase()
        switch(input) {
            case 'ALCOA':
                window.location.replace('alcoa-Forms.htm');
                break;

You need to convert your input to either lower or upper case. For example:

var input = document.getElementById('userInput').value.toLowerCase();

Use .toLowerCase() or .toLocaleLowerCase() Note that these functions are nearly identical with some obscure exceptions in languages such as Turkish.

Code

function redirect() {
     var input = document.getElementById('userInput').value.toLowerCase();
     switch (input) {
        case 'alcoa':
            window.location.replace('alcoa-Forms.htm');
            break;
    }
}

More detailed

A function is not "case sensitive". Rather, your code is case sensitive. The way to avoid this problem is to normalize the input to a single case before checking the results. One way of doing so is to turn the string into all lowercase before checking.

An alternate solution

Use the case fallthrough syntax:

switch(text) { 
     case 'a':
     case 'A':
         doSomething();
}

Though .toLowerCase() (or .toUpperCase() ) is the simplest way, there is also a regex way:

if (/^alcoa$/i.test(input)) {
    // ...
}

if you use toUpperCase() then inside switch(input) function case string should be all in upper case like below:

   var input = document.getElementById('userInput').value.toUpperCase();
    switch (input) {
       case 'ALCOA':
               // do something
               break;
    }

if you use toLowerCase() then inside switch(input) function case string should be all in lower case like below:

 var input = document.getElementById('userInput').value.toLowerCase();
switch (input) {
   case 'alcoa':
           // do something
           break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM