Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if form is submitted successfully html5

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.

like image 997
Junaid Atique Avatar asked Apr 24 '14 11:04

Junaid Atique


People also ask

How do I validate a form in HTML?

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.

What happens when HTML form is submitted?

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.

What is data validation in HTML?

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?


1 Answers

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"); 
});
like image 89
MrCode Avatar answered Sep 23 '22 15:09

MrCode