问题描述
我试图将两个字符串转换为相同的格式,例如toUpperCase/toLowerCase,以比较两个字符串,而不管javaScript中是否区分大小写.下面是我的功能.
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出现此错误Uncaught TypeError:无法读取未定义的属性'toUpperCase'.我也尝试先转换toString(),然后再转换为toLowercase().它也犯了错误(toString()未定义).如果没有其他区分大小写的方式来比较两个字符串,还建议我.谢谢!
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!
推荐答案
尝试一下:
var getusername= ""+usernames[k];
如果这不起作用,则可能是问题所在:
If that doesn't work, then this could be the problem:
切换
for (var k = 0; k <= usernames.length; k++)
为
for (var k = 0; k <= usernames.length-1; k++)
这篇关于未捕获的TypeError:无法读取未定义的属性"toUpperCase"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!