Trim whitespace on search input field

3 posts by 2 authors in: Forums > CMS Builder
Last Post: May 22, 2018   (RSS)

By gversion - May 22, 2018

Hello,

I am using the "fieldname_keyword" search type on my form and I have noticed that if a user searches for "television " (with a space at the end of the search phrase) then no results are returned. If the user searches for "television" then the correct results are returned.

What's the best way to handle this? Should I be trimming the whitespace using jQuery? Should I be using some PHP that removes any leading and trailing whitespace?

Any advice and sample code would be greatly appreciated!

Regards,

Greg

By gversion - May 22, 2018

Thanks, Leo!

I was trying to get the form to remove the trailing whitespace using JS with the following code:

    <script>
      $('#search_submit_button').click(function(){
      var text = $('#search-desktop').val();
      text = text.trim();
      $('#search-desktop').val(text);
      $("#header-search").submit();
      });
    </script>

Seems to be working ok...!

Regards,

Greg