简体   繁体   English

如何使用JavaScript将所有用户输入转换为大写字母?

[英]How can I convert all my user inputs to the upper case letter using JavaScript?

I have a JavaScript function that checks the user input and will compare it against the database. 我有一个JavaScript函数,可以检查用户输入并将其与数据库进行比较。

function checkUserInput() {
    var userInput = document.getElementById("textInput").value;
    var stringToCheckAgainst = random_images_array[num].split('.');

    if (userInput == stringToCheckAgainst[0]) {

        document.getElementById("row1").innerHTML = "<p>" + txt.fontsize(5) + "</p>";
        document.close();

    } else {
        //user has inputted an incorrect string
        document.getElementById("row1").innerHTML = "<p>" + txt1.fontsize(5) + "</p>";
        document.close();
    }
}

My problem is at the moment this function is case sensitive which means if the user types in “apple” it will return false statements as a fact that all of my objects are named with uppercase letters. 我的问题是,此函数目前区分大小写,这意味着如果用户键入“ apple”,它将返回错误的语句,因为我的所有对象都使用大写字母命名。 How can I make my function not case sensitive to works with both lower case and uppercase letters no matter how the user is typing in? 无论用户如何键入,如何使我的函数不区分大小写?

您可以这样使用:

if (userInput.toUpperCase() == stringToCheckAgainst[0].toUpperCase()) {

Try this: 尝试这个:

if(cmp_first.toUpperCase()===cmp_second.toUpperCase()){
    alert('Strings are Identical');
}else{
    alert('String mismatch');
}
var userInput = document.getElementById("textInput").value;
document.write(userInput.toUpperCase());

To convert a string to lowercase or uppercase letters, use: toLowerCase() and toUpperCase() methods. 要将字符串转换为小写或大写字母,请使用: toLowerCase()toUpperCase()方法。

var userInput = document.getElementById("textInput").value;
userInput.toLowerCase() // lowercase
userInput.toUpperCase() // UPPERCASE

-Use the toUpperCase() function. -使用toUpperCase()函数。

EXAMPLE: Desired_string.toUpperCase 示例: Desired_string.toUpperCase

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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