View Full Version : How do I require Minimum Search Characters
Pinto7
12-05-2005, 11:06 PM
I have been trying to develop a script that the user must enter a minimum amount of characters in order for the search to take place on the search.asp file.
I have tried to insert
If Request.Form<>"" then
If len(Request.Form("txtSearch"))>=5 then
Response.Write("Please use 5 or more characters to perform search")
But it does not work
Brandoe85
12-05-2005, 11:18 PM
Here you are saying if they enter at least 5 characters. So if this passes, you want to do the search:
If len(Request.Form("txtSearch")) >= 5 Then
' do search
Else
Response.Write("Please use 5 or more characters to perform search")
End If
Good luck;
Pinto7
12-05-2005, 11:25 PM
Brandoe85
Thanks for the help,
the problem now is , if I leave the search field on the form blank and submit, I get the Response.Write Statement, and the list of the entier database, which I dont want the user to see.
example:
Please use 5 or more characters to perform searchKurt
Maurina
Chyva
Samual
Pinto7
12-05-2005, 11:58 PM
The previous code causes the the statement to show up on every search, even on Blank user searches.
<%
Dim strInputSearch 'Variable for the search word
Dim strCon 'Holds the string to connect to the db
Dim adoCon 'Database Connection Variable Object
Dim strSQL 'Holds the SQL query for the database
Dim rsSearch 'Holds the search recordset
'This is the variable that has the search word
strInputSearch = Request.Form("txtSearch")
If len(Request.Form("txtSearch")) >= 5 Then
' do search
Else
Response.Write("Please use 5 or more characters to perform search")
End If
'This makes it so people cant inject SQL code and/or cause some unwanted
errors
strInputSearch = Replace(strInputSearch,"'", "''", 1, -1, 1)
'This sets the connection
Set adoCon = Server.CreateObject("ADODB.Connection")
'This is the connection string
strCon = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ="
&Server.MapPath("tester.mdb")
'Open the connection
adoCon.Open strCon
'Set the database connection
Set rsSearch = Server.CreateObject("ADODB.Recordset")
'This is the SQL statement for searching. You will need to change tblTable
and Field to match your database (they are in red)
strSQL = "SELECT * FROM Table1 WHERE data1 LIKE '%" & strInputSearch & "%';"
'Opens the database so we can get the results of the search
rsSearch.Open strSQL, adoCon
'43 If there are no matches then continue
If rsSearch.EOF Then
'Write out the message that there are no matches
Response.Write("There are no matches with your search")
'If there are matches, continue
Else
'Loop through the database to display all the matches
DO UNTIL rsSearch.EOF
'Write out the match found. You need to change Field to your
database field you are search (its in red)
Response.Write(rsSearch("data1") & "<br>")
'Move to next line in database
rsSearch.MoveNext
'Continue looping through database to display all results found
loop
End If
'Reset server objects
Set rsSearch = Nothing
adoCon.Close
%>
Brandoe85
12-06-2005, 12:04 AM
You should put all of your logic that does the search in that if statement that checks the length of the field. I just copied and pasted your current code, so you may have to clean it up a bit:
<%
Dim strInputSearch 'Variable for the search word
Dim strCon 'Holds the string to connect to the db
Dim adoCon 'Database Connection Variable Object
Dim strSQL 'Holds the SQL query for the database
Dim rsSearch 'Holds the search recordset
'This is the variable that has the search word
strInputSearch = Request.Form("txtSearch")
If len(strInputSearch) >= 5 Then
'This makes it so people cant inject SQL code and/or cause some unwanted
strInputSearch = Replace(strInputSearch,"'", "''", 1, -1, 1)
'This sets the connection
Set adoCon = Server.CreateObject("ADODB.Connection")
'This is the connection string
strCon = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ="
&Server.MapPath("tester.mdb")
'Open the connection
adoCon.Open strCon
'Set the database connection
Set rsSearch = Server.CreateObject("ADODB.Recordset")
'This is the SQL statement for searching. You will need to change tblTable
and Field to match your database (they are in red)
strSQL = "SELECT * FROM Table1 WHERE data1 LIKE '%" & strInputSearch & "%';"
'Opens the database so we can get the results of the search
rsSearch.Open strSQL, adoCon
'43 If there are no matches then continue
If rsSearch.EOF Then
'Write out the message that there are no matches
Response.Write("There are no matches with your search")
'If there are matches, continue
Else
'Loop through the database to display all the matches
DO UNTIL rsSearch.EOF
'Write out the match found. You need to change Field to your
database field you are search (its in red)
Response.Write(rsSearch("data1") & "<br>")
'Move to next line in database
rsSearch.MoveNext
'Continue looping through database to display all results found
loop
End If
'Reset server objects
Set rsSearch = Nothing
adoCon.Close
Else
Response.Write("Please use 5 or more characters to perform search")
End If
%>
Good luck;
Pinto7
12-06-2005, 12:12 AM
Brandoe85
You are a life saver !
It works perfect now.
Thanks
Brandoe85
12-06-2005, 01:17 AM
You're welcome :)
Glad you got it working.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.