MrFoh MrFoh - 1 year ago 89
HTML Question

How check if html5 form validation is complete

I have a form which am using html5 to validate for email and required fields.
The form is to be submitted via ajax.
How do i determine that the form passed validation before i allow to form to submitted via ajax.

This is my html:

<form action="" method="post" id="comment_form">
<input type="hidden" name="post_id" value="<?=$post->ID ?>" />
<label for="name">Name</label>
<input type="text" name="name" required="required">

<label for="email">Email Address</label>
<input type="email" name="email" required="required">

<label for="comment">Comment</label>
<textarea name="comment" required="required" >

<label for="submit"></label>
<button type="submit"class="button green meduim" id="comment_btn">Submit</button>

This is my jquery

//the request url
var url = site_url+'comments/post_comment';
//cross site request forgery token
var xtoken = $("input[name='xtoken']").val();
//post data
var dataObj = {
//dim form
//make request
error:function() {},
success:function(resp) {
if(resp.status == "ok")
}else {
alert("An error was encountered");

Is there a way to determine if the form is validated?

Answer Source

You can use the validator's form() function, e.g.

var validator = $("#comment_form").validate(options);
if (validator.form()) {
    // submit with AJAX
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download