Oh, assuming it was vanilla Javascript and not some fancy library, it would be something like this:
Your input will have an id on it, to match the label's for attribute, so let's pretend it's id="q".
...
<input type="text" id="q" name="searchiwhatever">
<input type="submit" value="Search">
...
Code:
var query;
if (query=getElementById('q')) { //only runs if q exists
q.onblur = function() {
if (q.value=='') return; //don't do anything if they left it blank
var pattern=/^[^"].*[^"]$/i;
if (pattern.test(q.value)) { //if what they typed doesn't start or end with quotes, add them
q.value='"' + q.value + '"';
}
}
}
...I think.
The pattern is very stupid, and it'll miss things, but you could make it more elaborate.
Right now, if there are no quotes at the beginning and end of the query, Javascript will wrap in quotes. Also if they do something like
foo "man choo" bar
they'll get
"foo "man choo" bar"
and if they start OR end with quotes, Javascript won't do anything, so if it's
everybody say "foo man choo"
or
"foo man choo" main chorus
it won't add the other quote
Another thing, a lot of people hit ENTER instead of moving the focus. I don't know if Javascript actually gets time to add the quotes in before it's sent to the form, but the user won't see the quotes added.
You could add in an event listener instead of just onblur(), listen for both onblur() and onkeyup of keycode 13... you see once you start using Javascript for stuff like this, you start wanting to really have it work for all sorts of instances, because users just do things various ways.
Since you'll be massaging this on the back end anyway, I dunno if you'd want to get too elaborate on the front end.
Bookmarks