Solved

check search item for symbols

Posted on 2003-10-29
6
264 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSS Scroll Issue 3 81
jQuery Parallax 1 38
Elegant Way to Include Query String When it exists? 4 42
Initialize SharePoint Online List Values in Quick Edit 7 20
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

932 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

9 Experts available now in Live!

Get 1:1 Help Now