Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap form input: prevent submit, but allow for input checking

I've got the following problem: I use bootstrap form to take input from users, and I use jQuery preventDefault() to disable the submit button from sending the form (I use AJAX instead). However, that function also prevents input checking that is done by bootstrap. For example, if someone enters an e-mail without '@' into

<input type="email" class="form-control">

bootstrap would, upon clicking submit, check that input and return a popup with an error.

My question is: how to prevent the request being sent while keeping the bootstrap form checking mechanism intact?

What I have tried: using preventDefault() and writing my own checking script, however this seems like reinventing the wheel and having extra code when it's not needed.

Thank you!

like image 412
Aroll605 Avatar asked Jun 20 '14 18:06

Aroll605


People also ask

How do I stop form submit if validation fails?

Use the return value of the function to stop the execution of a form in JavaScript. False would return if the form fails to submit.

Does bootstrap have form validation?

Here's how form validation works with Bootstrap: HTML form validation is applied via CSS's two pseudo-classes, :invalid and :valid . It applies to <input> , <select> , and <textarea> elements. Bootstrap scopes the :invalid and :valid styles to parent .was-validated class, usually applied to the <form> .

How do you validate a form?

Basic Validation − First of all, the form must be checked to make sure all the mandatory fields are filled in. It would require just a loop through each field in the form and check for data. Data Format Validation − Secondly, the data that is entered must be checked for correct form and value.

How do I get rid of please fill this field in HTML?

Error trigger: it's a known feature of Chrome 10, if required is present in input [1]. Solution: use formnovalidate in whatever button that triggers the prompt [2], or simply use novalidate in form tag.


1 Answers

I believe you are talking about the native HTML5 form validation and not validation by bootstrap its self, I have never come across bootstrap validation before. (i may be wrong though).

Most new browsers will validate <input type='email'/> as an email address and <input type='text' required='required'/> as required on form submission.

If for example you are using e.preventDefault(); on the click event on the submit button the form will never attempt to submit and hence the native validation will never happen.

If you want to keep the validation you need to use e.preventDefault(); on the submit event of the form not the click event on the button.

The html...

<form action='' method='post'>
   <input type='email' name='email' required='required' placeholder='email'/>
   <button type='submit'>Submit</button>
</form>

The jQuery...

//this will stop the click on the button and will not trigger validation as the submit event will never be triggered
$("button").on('click',function(e){
    e.preventDefault();
    //ajax code here
});

//this will stop the submit of the form but allow the native HTML5 validation (which is what i believe you are after)
$("form").on('submit',function(e){
    e.preventDefault();
    //ajax code here
});

Anyway hope this helps. If I have misunderstood in any way let me know and ill try to assist further.

like image 59
Ed Fryed Avatar answered Oct 04 '22 07:10

Ed Fryed