Convert all user input values to uppercase (or lowercase) first, and then compare:
var name = document.form1.txtUserName.value.toLowerCase(); if(name == "urname") { // statements go here. }
JavaScript has two equality operators. One is completely backward compatible, the standard "==". If the two operand types are inconsistent, it will automatically perform type conversion on the operand at some point. Consider the following assignment statement:
var strA = "i love you!"; var strB = new String("i love you!");
These two variables contain the same sequence of characters, but have different data types. The former is string and the latter is object. When using the "==" operator, JavaScript will try various evaluations to detect whether the two will be in the same sequence. equal in some cases. So the following expression evaluates to true: strA == strB.
The second operator is the "strict" "===", which is not so forgiving when evaluating and does not perform type conversion. So the expression strA === strB evaluates to false, although both variables hold the same value.
Sometimes the logic of the code requires you to determine whether two values are not equal. There are two options here: "!=" and strict "!==". Their relationship is similar to "==" and "===".
Discussion:
"==" and "!=" will try their best to find matching values when evaluating, but you may still want to perform an explicit type conversion before comparing to "help" them do their job. For example, if you want to determine whether a user's input value (string) is equal to a number, you can let "==" help you complete the type conversion:
if(document.form1.txtAge.value == someNumericVar) { ... }
You can also convert in advance:
if(parseInt(document.form1.txtAge.value) == someNumericVar) { ... }
If you are more accustomed to strongly typed programming languages (such as C#, Java, etc.), then you can continue your habit (type conversion) here, which will also enhance the readability of the program.
There is one thing you need to pay attention to, which is the computer’s regional settings. If you compare strings with "<" and ">", then JavaScript compares them as Unicode, but obviously people browsing the web don't read the text as Unicode :) For example in Spanish, According to traditional sorting, "ch" will be sorted as a character between "c" and "d". localeCompare() provides a way to use the character collation of the default locale.
var strings; // 要排序的字符串数组,假设已经得到初始化 strings.sort(function(a,b) { return a.localeCompare(b) }); // 调用sort()方法进行排序