0

When users search for 1,2 or 3 characters this puts a lot of strain on the database. A malicious user could search for 1,2 or 3 characters and bring my site down to its knees.

I presume I could use some Jquery code to prevent users entering less than 4 characters when searching.

   <form method="get" id="searchform" action="<?php echo home_url(); ?>/">                             
                            <input type="text" value="<?php _e( 'search', 'swagger' ); ?>" onfocus="if (this.value == '<?php _e( 'search', 'swagger' ); ?>') {this.value = '';}" onblur="if (this.value == '') {this.value = '<?php _e( 'search', 'swagger' ); ?>';}" name="s" id="s" />          
                        </form> 

any suggestions?

1
  • "A malicious user" would also know how to easily bypass this - if you want to protect your server - check on the server-side ! – jave.web Nov 29 '19 at 16:26
1

You can use the pattern attribute to ask for at least 3 characters:

<input pattern=".{3,}">

But if your database dies just because someone searches for a three letter word … you have other problems than search. Optimize your database instead, or find another solution. A simple search, even dozens per minute should not be a problem.

To catch empty searches, see my article on WPEngineer: Fix Empty Searches

2
  • ok this works but it doesn't prevent users entering nothing and pressing enter – Mario Lorenzo May 23 '13 at 21:47
  • @MarioLorenzo See my update. – fuxia May 23 '13 at 21:51

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.