Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using search to find quotes

Status
Not open for further replies.

Helen267

Programmer
Sep 2, 2003
25
NZ
I'm trying to write a simple function to search for quotes in a CF form field (called heading).

I suspect there is some special syntax required around using quotes, since they are special characters in JS, but I'm self-taught and having trouble finding any references!

My function is as follows (executed using onClick on the form submit button):

Code:
<SCRIPT LANGUAGE="JavaScript">
<!--

function checkform(form) 		
  {
    if (form.heading.value.search(/\'/g));
    {
    alert("Please do not use quotes in your heading");
    form.heading.focus();
    return false;
    }
  }
	
// -->
</SCRIPT>

The alert pops up regardless of whether a quote is used in the field or not...

What am I doing wrong?

I'd also like to search for double quotes ("), so any help there would also be appreciated.

Thanks in anticipation.....

Helen
 
the [tt]search[/tt] method returns an integer. It's always going to be true. Either check to see if the result of the search is less than zero, or use the [tt]test[/tt] method of the regular expression object to return a true/false result.

For double quotes just change your regular expression.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]
 
Thanks - I knew I was missing something obvious!

My code is now working fine for the single quote, but I'm having a little trouble with the double quote (") - presumably because it's a reserved character in JS.

Is there some other syntax I should use besides this:

Code:
if (str.search(/\"/g) < '0')

Thanks for your help on this...
 
I don't think there should be quotes around the 0: you are comparing it to the number 0, not the string "0".

--Chessbot

"So it goes."
Kurt Vonnegut, Slaughterhouse Five
 
Thanks, that's what I needed. It's still not displaying correctly in CF (which uses colour coding) but at least it's working!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top