views:

278

answers:

3

In JavaScript, I'm trying using the user's input to search my database. For example, user input is "monster", and my database's data is "Monster". How can I have it match regardless of it's casing?

+1  A: 

Javascript string case-insensitive comparisons can be performed with string.toUpperCase.

if (input.toLowerCase() === "other string")
    ....

(I'm assuming your database example is just an example as databases usually ignore the case of strings :)

CrazyJugglerDrummer
You forgot to call the function.
SLaks
There's no value of `input` such that `(input.toUpperString == "other string")`, since `"other string"` has lower case characters
Dancrumb
@Dancrumb thanks, and thanks to svinto too for the edit. I was tired... :P
CrazyJugglerDrummer
A: 

You should convert both javascript string and database where clause to use lower case string.

But I guess database like sql server and mysql are all case insensitive in terms of string.

sza
A: 

If you're using AJAX then you're using a server side language. Why don't you let the server side script normalize the data?. You could even delegate this task to the database, using the proper UPPER and LOWER functions, but for security reasons data normalization should be a task for the server side script.

You can use JS to pre-check the data in terms of length and syntaxes, mostly for helping and preventing the user from making mistakes, but one thing you should never do is query untreated JS data. Anyone could manipulate the JS and query a' OR 1=1; DROP TABLE users;--, even if you validate the data.

Ben
Obligatory: http://xkcd.com/327/
MatrixFrog