Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

searching for a string/characters within a listbox...

Status
Not open for further replies.

justme123

Programmer
Oct 9, 2003
14
0
0
US
Hi, I have a text box and a listbox...in my JSP the listbox values get populated frm the database... i have a text box where the user can enter a string and on click of the filter button, it has to show me a new list with the list containing only the string/characters that the user has entered in the text box....can somebody help me with a script for implementing this?.... i would appreciate all your help.... thanks and regards
 
>> in my JSP the listbox values get populated frm the database

Start by getting the JSP to populate an array rather than the list box directly.

Then you can loop through the array testing each value against the filter in the text box and creating the list box options as appropriate.

Do you want the filter to match only the beginning of the value or match anywhere in the value?

Either way, using regular expressions is a quick way of matching strings against substrings.


[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
thanx much for the response!...

i want to filter to match anywhere in the value...
can u give me an example of using regex to implement this?...i would appreciate it very much!...

thanks again
 
Sure:
Code:
<html>
<head>
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
//These would get populated from your database
var selectValues = new Array(&quot;Aaron&quot;,&quot;Barbara&quot;,&quot;Colin&quot;,&quot;Darryl&quot;,&quot;Edith&quot;,&quot;Frank&quot;,&quot;Genevive&quot;,
                             &quot;Harold&quot;,&quot;Ivy&quot;,&quot;Jack&quot;,&quot;Karen&quot;,&quot;Larry&quot;,&quot;Martine&quot;,&quot;Oscar&quot;,&quot;Paula&quot;,
							 &quot;Quentin&quot;,&quot;Ruth&quot;,&quot;Samuel&quot;,&quot;Tina&quot;,&quot;Ursula&quot;,&quot;Vanessa&quot;,&quot;William&quot;,&quot;Xavier&quot;,
							 &quot;Yasmine&quot;,&quot;Zoe&quot;);
							 
function filterSelect(strSelectId, objArray){
 var objSelect = document.getElementById(strSelectId);
 var objFilter = document.getElementById('filterField');
 var objCaseSwitch = document.getElementById('caseSwitch');
 //Check if the filter field has anything in it
 if(objFilter.value.length > 0){
  //Yep, build a regular expression based on the value
  var caseSensitive = 'gi';
  if(objCaseSwitch.checked){
   caseSensitive = 'g';
  }
  var regExpr = new RegExp(objFilter.value, caseSensitive);
 }
 // clear out any existing options in the select
 while(objSelect.options.length > 0){
  objSelect.options[0] = null;
 }
 // loop through the array
 for(var i = 0; i < objArray.length; i++){
  //if the filter field has anything in it
  if(objFilter.value.length > 0){
   //test the regular expression against the array value
   var patternMatch = regExpr.test(objArray[i]);
   // if we have a match
   if(patternMatch){
    //add the value to the select
	objSelect.options[objSelect.options.length] = new Option(objArray[i]);
   }
  }
  // if no filter specified, just add all elements
  else{
   objSelect.options[objSelect.options.length] = new Option(objArray[i]);
  }
 }
}
</script>
</head>
<body onload=&quot;filterSelect('selectField', selectValues);&quot;>
Enter Filter Text: <input type=&quot;text&quot; id=&quot;filterField&quot;>
<input type=&quot;button&quot; onclick=&quot;filterSelect('selectField', selectValues);&quot; value=&quot;Go&quot;><br>
<input type=&quot;checkbox&quot; id=&quot;caseSwitch&quot;>Case Sensitive?<br>
<select multiple id=&quot;selectField&quot;>
</select>
</body>
</html>

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
just saw ur post...will try what u r suggesting and will let you know...

thanks a bunch for helping me out!...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top