Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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

share|improve this answer
    
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. –  toscho May 23 '13 at 21:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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