PHP Code:
<html>
<head>
<title>
Javascript Testing
</title>
</head>
<body>
<form name="myForm">
<table>
<tr><td>Name:</td><td><input type="text" name="nameget" onkeyup="suggest();"></input></td></tr>
<tr><td>Suggestion:</td><td><div id="suggestion"></div></td></tr>
</table>
</form>
<script type="text/javascript">
var dictionary;
function pullData() // Pull the words from the file to an array named dictionary.
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
return false;
}
}
}
var getsrc = "dictionary.txt"; // File to pull words from
xmlHttp.open("GET",getsrc,false); // Open the file
xmlHttp.send(null);
var dictresponse = xmlHttp.responseText; // Get response
dictionary = dictresponse.split("\n"); // Articulate - newline is separator
}
pullData(); // Execute
function suggest() // Suggests names based on user input
{
document.getElementById('suggestion').innerHTML = ""; // Clear the div
for (i=0;i<dictionary.length;i++) // Loop through each value in the dictionary array
{
var match = document.myForm.nameget.value; // Pass to variable for regex evaluation
match = "^" + match; // Must be at the beginning
if ((dictionary[i].search(match) != -1) && myForm.nameget.value != '') // If a match is found...
{
document.getElementById('suggestion').innerHTML += dictionary[i] + "<br>"; // ... display it in the "suggestion" div
}
}
}
</script>
</body>
</html>
My problem is in the suggest() function. "(dictionary[i].search(match)" to be specific. How can I make this search case-insensitive? It seems that I can't use "search(/match/i)" because match is a variable.
I've tried applying various examples on the web to no avail. Any suggestions?
Bookmarks