7

I have the following form. I like the new HTML5 form validation and I would prefer to keep it. However. I don't like the way that when the button is pressed it refreshes the page (form submit).

Instead I would prefer to use the button to trigger some AJAX to refresh page elements without refreshing the entire page. However, when I set type="button" what happens is that the HTML5 form validation ceases to trigger when the button is pressed.

How can I use HTML5 form validation, while not triggering propagation of refreshing/submitting the page?

Note that I'm not concerned with the AJAX elements of this problem, just the HTML5 validation issue.

echo "<form>";
echo "<td><input id=\"link_add_title\" type=\"text\" class=\"form-control\" placeholder=\"URL Title\"></td>";
echo "<td><input id=\"link_add_url\" type=\"url\" class=\"form-control\" placeholder=\"Your URL\"></td>";
echo "<td><input id=\"link_add_budget\" type=\"number\" step=\"any\" class=\"form-control\" placeholder=\"Budget\"></td>";
echo "<td><button class=\"btn btn-sm btn-success\"><i class=\"fa fa-check\"></i> Add</button></td>";
echo "</form>";
Amy Neville
  • 10,067
  • 13
  • 58
  • 94

2 Answers2

14

This way you prevent actual submit, but HTML5 validation triggers:

$('form').submit(function(event){

  event.preventDefault();

});

As Samveen points out, this way should be preferred over listening to the onclick event of the <button>/<input type="submit"> element, because the latter would prevent HTML5 validation in addition to normal form submit - see fiddle.

Community
  • 1
  • 1
moonwave99
  • 21,957
  • 3
  • 43
  • 64
  • That works thank you! Obviously I will improve the selectors though so it doesn't apply to all forms :) – Amy Neville Nov 16 '13 at 16:55
  • thanks for this answer! i was going crazy over jquery Ajax html5 form validation not triggering when submitting form, but triggering after the form was submitted. – Rahul Patwa Mar 18 '14 at 23:40
  • 1
    A small addendum: The usual paradigm is to do form submission on button click event: `$('button').click(function(evt){\*AJAX*\});` which needs to change to the above mentioned form submit event: `$('form').submit(function(event){\*AJAX*\});` in conjunction with altering the button type back to `type="submit"` – Samveen Oct 07 '15 at 11:01
  • 2
    @Samveen: with `$(...).click()`, [you prevent HTML5 validation from happening](http://jsfiddle.net/ozrjbLwo/). So please listen to `submit` only : )) – moonwave99 Oct 07 '15 at 16:47
  • 1
    @moonwave99: What you are saying is exactly what I discovered yesterday (the problem that lead me to this answer). All my form related code **incorrectly** listens to `$('button').click()`, this unfortunate paradigm being what I learned from searching on Google and SE(see [other answer](http://stackoverflow.com/a/20021021/1353267)). Thus my comment to point out the `gotcha` that got me. – Samveen Oct 08 '15 at 04:13
  • @Samveen oh sorry, I understood the other way around! Thank you then ^^ – moonwave99 Oct 08 '15 at 14:26
  • 1
    @moonwave99, Would you the relevant parts of my comment as a note into your answer? It would make a great answer even better :) (something to the effect of **not** using `$('button').click()`, and using `$('form').submit(...)` instead?) – Samveen Oct 12 '15 at 06:49
  • @Samveen sure, I just did! – moonwave99 Oct 12 '15 at 09:28
  • I have problem with validation reset using html5 but it is not working using reset(). you can check using firefox... there is after submit border not cleared. `http://jsfiddle.net/KDkvE/` i have trying to ask new question but its band dont know why – bharat Feb 08 '17 at 13:58
1

With a submit button try..

html
<input type="submit" class="your-button-class" />

js
$(document).on('click','.your-button-class',function(){
 //your code

 return false;  //this will prevent the page refresh
});
Achilles
  • 114
  • 4
  • 11