Solved

check search item for symbols

Posted on 2003-10-29
6
261 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now