2

I'm using custom search for my website. Now I wan't to validate user input before search like validate white-spaces (just enter space in search box and you'll find all things goes wrong).

For minimum length validation drupal take cares for minimum 3 character, but if you enter less then 3 characters and press search button search results page appears and then it shows an error that you need to enter keyword with 3 characters or more.

I don't want to go on search results page until all validation criteria passes. In this case I want just red border on search box (this is happen when no text inserted in the search box and you press search button).

How can I validate white-space, minimum search keyword length and restrict user on same page until all validation criteria passes in custom search module?

0

You can use hook_form_alter(&$form, &$form_state, $form_id) and extend your validation process by adding $form['#validate'][] = 'custom_validation()'

0

Found solution.. :) You need to modify custom search js file for achieve this. This file can be found at sites\all\modules\custom_search\js\custom_search.js. Edit this file as following..

if (!Drupal.settings.custom_search.solr) {
        // Check if the search box is not empty on submit
        $('form.search-form', context).submit(function(){
          var box = $(this).find('input.custom-search-box');

       //Search Box Patch Start

      var searchBox=$.trim(box.val());
          var searchLen = searchBox.length;     

          if(searchLen < 3)
          {
            searchBox = "";
          }
          if (searchBox != undefined && (searchBox == '' || searchBox == $(this).find('input.default-text').val())) {
            $(this).find('input.custom-search-box').addClass('error');
            return false;
          }

          //Search Box Patch End

Here I edited this file for minimum 3 characters to search (without white-space) and if this criteria fails then no redirecting on search results page, you'll find red border on search box on the same page. This is my requirement, but you can edit file this as per your desire output.

(NOTE: Make sure that you disable Block Lazyloader module for use custom_search.js otherwise it will not work).

Hope this will help you...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.