Uncaught TypeError: Cannot read property 'toUpperCase' of undefined

南楼画角 提交于 2019-12-23 22:01:26

问题


I am trying to convert two strings to same format like toUpperCase/toLowerCase to compare two strings regardless of case sensitive in javaScript. Below is my function.

    function submitForm() {

    var usernames=['one','two','Test'];
    var cpusername = "test";
            var flag = 0;

   if (cpusername !== "")
                {
    for (var k = 0; k < usernames.length; k++)
                {
        var upperCasecpusername=cpusername.toUpperCase();
        var getusername= usernames[k];
        var upperCaseusername=getusername.toUpperCase();

                    if (upperCasecpusername === upperCaseusername)
                    {
                        flag=1;
     console.log(flag);
            //document.getElementById('cpusername').value = '';
                $.messager.alert("Message", "Someone already has username"+cpusername+". Try another!!", '');                    


                    }
                }
                }

am getting this error Uncaught TypeError: Cannot read property 'toUpperCase' of undefined .I have also tried to convert toString() first and then to toLowercase() .It was also erred (toString() undefined). Also suggest me if any other ways to compare two string regardless of case sensitive. Thanks!


回答1:


Try this:

var getusername= ""+usernames[k];

If that doesn't work, then this could be the problem:

Switch

for (var k = 0; k <= usernames.length; k++)

for

for (var k = 0; k <= usernames.length-1; k++)



回答2:


Try This

js code

jQuery( document ).ready(function() {

    var usernames=['one','two','Test'];
    var cpusername = "test";
            var flag = 0;

   if (cpusername !== "")
                {
    for (var k = 0; k < usernames.length; k++)
     {
        var upperCasecpusername=cpusername.toLowerCase();
        var getusername= usernames[k];
        console.log(getusername);
        var upperCaseusername=getusername.toLowerCase();

            if (upperCasecpusername === upperCaseusername)
           {
                    console.log(upperCasecpusername);
                    console.log(upperCaseusername);
                        flag=1;
                     console.log(flag);
            //document.getElementById('cpusername').value = '';
                alert( "Someone already has username"+cpusername+". Try another!!", '');                    


             }
       }
    }
});

You can view demo here




回答3:


Using functions .toLowerCase() & .toUpperCase() are fine, but as requested to other way to string case insensitive comparison, I would recommend to use regex. Try something like this for case insensitive string comparison

var str= "TEST";
var result = str.match(/test/i);
if(result){
// Write logic once the match found
}


来源:https://stackoverflow.com/questions/35643980/uncaught-typeerror-cannot-read-property-touppercase-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!