1

I would like to enclose a search term with Double quotes automatically

For e.g.

<input type="text" name="keywords" value="" id="search" size="17" />
<input type="submit" value="Go">

a visitor tries to search for a phrase = long enough (it contains 2 words with a space in between)

I need the string long enough to be passed as "long enough" to server side (PHP)

I dont want to tell the visitor to use Double quotes for searching for a phrase, for the visitor the search should be easy.

Presently, when a visitor enters a search term with one word long the search results show correct articles.

But when a visitor uses a phrase long enough or any other phrase, using a minimum of 2 words, the search results are not accurate as the search take long enough as 2 seperate search terms.

For accurate search results, the ExpressionEngine CMS wants the visitor to enter "long enough" [any phrase to be enclosed in double quotes]

This is what i want to do automatically, when a visitor searches for one word, it should leave the keyword and let it pass without any quotes, but when a visitor uses 2 or more words, the search form should pass those phrases in Double quotes


Code provided by @Jonathon Wisnoski works as i want it, but with one drawback

 <script type="text/javascript">
        function quoteWords() {
            var search = document.getElementById("search_box");
            search.value = search.value.replace(/^\s*|\s*$/g, ""); //trim string of ending and beginning whitespace
            if(search.value.indexOf(" ") != -1){ //if more then one word
                search.value = search.value.replace(/^"*|"*$/g, "\"");
            }
        }
  </script>

Issue : It breaks when manually adding double quotes and pressing submit, one extra double quote is entered at the end. The regex code should see if the double quotes exist, it should not add any thing.

So it makes "long enough" to "long enough""

Can anyone check the regex code so see how to solve this issue.

4
  • 2
    The term long enough should appear on the server as a single string. If you really want to wrap that single string in double quotes, so that the quotes are part of the string, just add them to the front and back. Unless I am mis-understanding your question? Commented May 27, 2011 at 1:24
  • 1
    Why can't your PHP script do this? Anything done on the client can be circumvented. Also, you're not doing this in order to interpolate it into a string of SQL, right? Commented May 27, 2011 at 1:25
  • @Jeremy Heiler, i have just added more information to the original question. Commented May 27, 2011 at 1:40
  • Why not do this on the server? Do you have access to the server code to make changes? Commented May 27, 2011 at 1:50

3 Answers 3

3

You should really just handle the string as is server side. Anything preprocessed with JavaScript can't be relied on.

However, if you did want to do this, there is a non standard String method called quote().

However, because it is non standard, I would use...

str = '"' + str.replace(/^"*|"*$/, '') + '"';

This strips leading and trailing " and then wraps the string again in ".

Sign up to request clarification or add additional context in comments.

4 Comments

How would i use the code, i only have HTML search input fields.
Correct me if I am wrong, I am not a regex expert at all, but this should be better (and you forgot the "g"): str = str.replace(/^"*|"*$/g, '"');
I incorporated my changed javascript into your code Ibn and added as an answer, hopefully it helps.
@Jonathon Yeah, it would be, guess I'm not thinking as well as I should be today :)
2

Since you're trying to validate the input to some extent, it might make more sense to do something a little more general - for example, if the user enters "foo "bar", you presumably want it to end up looking like "foo bar". So instead of:

str = '"' + str.replace(/^"*|"*$/g, '') + '"'

You might want to do something like:

str = '"' + str.replace(/"/g, '') + '"'

And that's a lot simpler to look at later.

1 Comment

This is by far the best solution. Thanks alot. It works perfectly. Ill post here, if an issue arises.
1

This should do it:

<input type="text" name="keywords" value="" id="search" size="17" />
<input onClick='var search = document.getElementById("search"); search.value = search.value.replace(/^\s*|\s*$/g, ""); if(search.value.indexOf(" ") != -1){search.value = search.value.replace(/^"*|"*$/g, "\"")}' type="submit" value="Go">

Or with the script in a script tag:

<script type="text/javascript">
    function quoteWords() {
        var search = document.getElementById("search");
        search.value = search.value.replace(/^\s*|\s*$/g, ""); //trim string of ending and beginning whitespace
        if(search.value.indexOf(" ") != -1){ //if more then one word
            search.value = search.value.replace(/^"*|"*$/g, "\"");
        }
    }
</script>
<input type="text" name="keywords" value="" id="search" size="17">
<input onClick="quoteWords()" type="submit" value="Go">

18 Comments

hmm, well test and get back to you.
Also note that I changed it so that it should only add teh quotes when more then one word is used.
@Jonathon Wisnoski, still did not work even with your changed code. The quotes are not being passed, they only pass when i add the double quotes manually into the search field "long enough"
Ok, tested and works (had to change how I used quotes in the code). I did not test for submitting it to a server, but I am sure that will work out fine.
Not that it should matter, but I never used the single quotes (') this way before, so I should say I tested in Firefox 3.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.