Solved

check search item for symbols

Posted on 2003-10-29
6
268 Views
Last Modified: 2008-02-07
hi guys need ur help....

i currently using asp for my system and i am also using javascript in validating one of the search function....

well, when the user type in other unnesscary symbols like @##$% or watever..
the system will juz replied "no record Found"

however when the user put in ' this symbol..the system will generate an error..
this could be due to the fact that i am using asp for sql query for the search thingy...

i tried using the codes below...but it will alert the error msg when the user click the mouse on other area..however if the system type enter instead...the error will come out again...
how can i solve this thing?

function checkSearch()
{
var string=document.SearchForm.Search.value
if (string.indexOf("'")==1 )
      {
      alert("Please do not use invalid symbols!")
      document.SearchForm.Search.focus()
      }
}

0
Comment
Question by:knighto
6 Comments
 
LVL 3

Expert Comment

by:superslamwich
ID: 9647386
I would suggeset changing it to:

function checkSearch()
{
var string=document.SearchForm.Search.value
if(string.indexOf("'")==1)
    {
    alert("Please do not use invalid symbols!")
    document.SearchForm.Search.focus()
    }
}


the .indexOf() property either says -1 if there is no value, or a number (0-n) of where the value is in the string.

if(string.indexOf("'")==1)  will only go if the ' mark is the second letter in the string.

{Slam}
0
 
LVL 3

Expert Comment

by:superslamwich
ID: 9647454
whoops, lol, forgot to change it.  Make it:
if(string.indexOf("'")>-1)
0
 

Author Comment

by:knighto
ID: 9647599
thanx for edit...
but however that's not reali wat i wanted

"
i tried using the codes below...but it will alert the error msg when the user click the mouse on other area..however if the system type enter instead...the error will come out again...
how can i solve this thing?
"
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 7

Expert Comment

by:Xxavier
ID: 9647635
<script>
function checkSearch(string)
{

if  (/[@$&%\.]/.test(string))
     {
     alert("Please do not use invalid symbols!")
   
     }
}
checkSearch('asdnfh#.ds$%sdghsgfd')
</script>

Use this regex, put the list to be tested for between the [ ]. Some of the chars mist be escaped out , ie
.\/*?()+,^| =!{}[]   must be ie as \. \\ \/ \* \?  etc

if you just want alpha numeric and a few special chars there is a slicker way of doing it, but this is a general technuique.

Xx
0
 
LVL 11

Accepted Solution

by:
Zontar earned 25 total points
ID: 9647654
Use a regular expression to match any non-letters, non-digits, etc.

function checkSearch(input)
{
  var value = true;
  var notValidChars = /[^a-z0-9 ]/i;

  if( notValidChars.test(input.value) )
  {
    alert("Please do not use invalid symbols!");
    input.focus();
    value = false;
  }

  return value;
}

If you don't want to allow digits, remove the 0-9 in notValidChars. If you don't want to allow spaces, remove the space from notValidChars.

> but it will alert the error msg when the user click the mouse on other area..however if the system type enter instead...the error will come out again...

How are you calling the function? Something like

<form onsubmit="return checkSearch(this.Search);">

should do the trick.
0
 

Expert Comment

by:Cleavis
ID: 10898624
I can't get this to work. what went wrong?

<SCRIPT>
function checkSearch(input)
{
var value = true;
var notValidChars = /[^a-z0-9]/i;

if( notValidChars.test(input.value) )
{
alert("Please do not use invalid symbols!");
input.focus();
value = false;
}
return value;
}
</SCRIPT>

<body>
<form method="post" action="/cgi-bin/check.cgi" onsubmit="return checkSearch(this.Search)">
<p>&nbsp;</p>
<p><input type="text" name="T1" size="20"></p>
<p><input type="submit" value="Submit" name="B1">
<input type="reset" value="Reset" name="B2"></p>
</form>
</body>
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question