Solved

check search item for symbols

Posted on 2003-10-29
6
271 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
[X]
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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

691 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