最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I make case function non case sensitive? - Stack Overflow

programmeradmin0浏览0评论
        <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?

        <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?

Share Improve this question edited Jan 7, 2013 at 17:58 jamesplease 12.9k6 gold badges50 silver badges76 bronze badges asked Jan 7, 2013 at 17:56 RolfRolf 7252 gold badges8 silver badges9 bronze badges 1
  • I guess no one caught on that the whole benefit is to make the case options readable like camel case yet keep them insensitive. I guess there is no way to do this. – KingOfHypocrites Commented Jun 2, 2013 at 23:29
Add a comment  | 

6 Answers 6

Reset to default 9

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;
}
发布评论

评论列表(0)

  1. 暂无评论