Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

check search item for symbols

Posted on 2003-10-29
6
269 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

839 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