html - GET request to same page- jQuery form -
i building small search engine, using form like
form action="" method="get" id="searchform"> <input type="search" name="search" id="searchquery" value="<?php echo $_get['search']; ?>" /> </form>
i passing search query same page. want prevent submitting form if text box value null. so, added event handler form
$('#searchform').submit(function() { alert('handler .submit() called.'); if($('#searchquery').val()==null) { return false; } });
but doesnt seem work, when try using form action ="#" works fine. can suggest workaround?
instead of checking against null, trim value , check against empty string.
$('#searchform').submit(function(event) { if($('#searchquery').val().trim()=="") { return false; } });
note: using trim() ensures form doesn't submit if whitespace in input text box.
Comments
Post a Comment