I have following code.
<div class="form-group">
<label for="store_account_name">Store Title</label>
<input type="text" value="" class="form-control" name="store_account_name" id="store_account_name" placeholder="Please insert store title of the external store" required />
</div>
<div class="form-group">
<label for="store_url">Store URL</label>
<input type="url" value="" class="form-control" name="store_url" id="store_url" placeholder="Please insert store URL as described in help section" required />
</div>
On submit button I want to check if form is validated. I have an listner event which has preventDefault button. So I want to send ajax call only when both of these inputs are successfully validated. Is there any JS function to check the status of validation or any css property is add to invalid field.
To validate the form using HTML, we will use HTML <input> required attribute. The <input> required attribute is a Boolean attribute that is used to specify the input element must be filled out before submitting the Form.
Most HTML forms have a submit button at the bottom of the form. Once all of the fields in the form have been filled in, the user clicks on the submit button to record the form data. The standard behaviour is to gather all of the data that were entered into the form and send it to another program to be processed.
Data validation is the process of ensuring that user input is clean, correct, and useful. Typical validation tasks are: has the user filled in all required fields? has the user entered a valid date? has the user entered text in a numeric field?
You should use the form's onsubmit
event. It sounds like you are listening on the submit button's click event which isn't right.
The form's submit event will only fire if the form is valid. The HTML5 validation rules will prevent this event from firing if the form is invalid.
jsFiddle demo
$('#myform').submit(function(e){
e.preventDefault();
// do ajax now
console.log("submitted");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With