2016-01-31 2 views
1

Мой друг работает над интересным веб-приложением Javascript, где вы вводите что-то в текстовое поле, и компьютер возвращает вам результат, но он чувствителен к регистру. Может ли он сделать это без учета регистра? Мы попытались использовать:Как сделать ввод текста в виде чувствительного текста?

var areEqual = string1.toUpperCase() === string2.toUpperCase(); 

, который был на JavaScript case insensitive string comparison, но он не может понять, как использовать.

function isValid() { 
    var password = document.getElementById('password').value; 

    if (password == "Shut Up") 
     { alert('HOW ABOUT YOU!') } 

    else if (password == "No") 
     { alert('You just did') } 

    else if (password == "Okay") 
     { alert('Okay') } 

    else if (password == "DH36 Rocks") 
     { alert('Check out his channel at www.youtube.com/dragonhe36!') } 

    else if (password == "LFH Sucks") 
     { alert('We do not accept haters here.') } 

    else if (password == "Hello") 
     { alert('Hi') } 

    else if (password == "Bored") 
     { alert('Then check out www.youtube.com/laughforhelp!') } 

    else if (password == "I subed to LFH") 
     { alert('Congrats!') } 

    else if (password == "I am an idiot") 
     { alert('No youre not... Unless youre Giovanni.') } 

    else if (password == "Hi") 
     { alert('Hello') } 

    else if (password == "Bye") 
     { alert('Nooooo...') } 

    else if (password == "Is 0.999... equal to 1?") 
     { alert('Yes') } 

    else if (password == "Yes") 
     { alert('Um... Am I missing something') } 

    else if (password == "Go away") 
     { alert('No.') } 

    else if (password == "I Like Pizza") 
     { alert('So do I but I prefer tacos') } 

    else if (password == "...") 
     { alert('...') } 

    else if (password == "Password") 
     { alert('My password is much more secure') } 

    else if (password == "Typo") 
     { alert('Waht? Where?') } 

    else if (password == "Type Something In") 
     { alert('Type Something In') } 

    else if (password == "Bonjour") 
     { alert('Comment ça va?') } 

    else if (password == "DH36 Sucks") 
     { alert('I disagree') } 

    else if (password == "I agree") 
     { alert('I disagree') } 

    else if (password == "Give Me Your Password") 
     { alert('I forgot it') } 

    else if (password == "Is this a robot?") 
     { alert('No, but maybe you are') } 

    else if (password == "Qwertyuiop") 
     { alert('Asdfghjkl') } 

    else if (password == "I pooped my pants") 
     { alert('I am glad to be a robot') } 

    else if (password == "I peed my pants") 
     { alert('At least it is not water!') } 

    else if (password == "Nothing") 
     { alert('That was something') } 

    else if (password == "This Website Sucks") 
     { alert('You forgot the period.') } 

    else if (password == "This Website Sucks.") 
     { alert('You forgot the does not inbetween Website and Sucks') } 

    else if (password == "When Is This Website Going To Be Edited?") 
     { alert('LFH is deciding') } 

    else if (password == "DumbHead36") 
     { alert('How did you know my password?') } 

    else if (password == "That Is Your Password?") 
     { alert('No.') } 

    else if (password == "What Is Your Password?") 
     { alert('I forgot it.') } 

    else if (password == "No It Was Not") 
     { alert('Yes It Was.') } 

    else if (password == "A") 
     { alert('BCDEFGHIJKLMNOPQRSTUVWXYZ') } 

    else if (password == "asd") 
     { alert('That is what I name most of my files.') } 

    else if (password == "123") 
     { alert('911') } 

    else if (password == "911") 
     { alert('What is your emergency?') } 

    else if (password == "I am bored") 
     { alert('So check out www.youtube.com/laughforhelp') } 

    else if (password == "9+10") 
     { alert('21') } 

    else if (password == "I Do Not Want To") 
     { alert('Wht Not?') } 

    else if (password == "Cause I Do Not Want To") 
     { alert('Why Not?') } 

    else if (password == "I Got Hurt") 
     { alert('Well, we cannot help you.') } 

    else if (password == "Why Not?") 
     { alert('Because.') } 

    else if (password == "www.youtube.com/laughforhelp") 
     { alert('It is so epic!') } 

    else if (password == "No It Is Not") 
     { alert('Yes It Is.') } 

    else if (password == "My Name Is LFH") 
     { alert('IMPOSTER ALERT!') } 

    else if (password == "www.laughforhelp.com") 
     { alert('That is the Future!') } 

    else if (password == "www.laughforhelp.16mb.com") 
     { alert('You are on that site right now.') } 

    else if (password == "Something Else") 
     { alert('Really?') } 

    else 
     { alert('Type Something Else') } 
} 

ответ

0

Просто добавьте .toUpperCase() за ваши переменные:

else if (password == "No") 
    {alert('You just did')} 

становится:

else if (password.toUpperCase() == "No".toUpperCase()) 
    {alert('You just did')} 

или просто:

else if (password.toUpperCase() == "NO") //Notice that "NO" is upper case. 
    {alert('You just did')} 
+0

Спасибо, это сработало очень хорошо для меня и моего друга, все вы можете получить от меня большие пальцы, это решило мою проблему. Спасибо, много парней! :) – dragonhe36

0

Вы можете использовать это, таким образом, например, в первом else-if bl Ock:

else if (password.toUpperCase() == "No".toUpperCase()) 
{alert('You just did')} 

Функция toUpperCase, применяется к строке, возвращает это верхний регистр версии, так что для Нет, это было бы NO. Если в переменной пароля содержится комбинация слов «нет», «нет», «нет», «нет» или «нет», то пароль.toUpperCase() будет «НЕТ». предыдущий код эквивалентен

else if (password.toUpperCase() == "NO") 
{alert('You just did')} 
0

Пожалуйста, если вы собираетесь сделать это, используйте switch! Единственное реальное различие между toLowerCase в отличие от toUpperCase здесь состоит в том, что значения в строках дела не будут кричать на вас.

function isValid() { 
    var password = document.getElementById('password').value; 

    switch (password.toLowerCase()){ 
     case "shut up": 
      alert('HOW ABOUT YOU!'); 
     break; 
     case "no": 
      alert('You just did'); 
     break; 
     case "okay": 
      alert('Okay'); 
     break; 

     // ... 

     case "something else": 
      alert('Really?'); 
     break; 
     default: 
      alert('Type Something Else') 
    } 
} 
Смежные вопросы