I am using the 'required' attribute in my form child tags for validation. The problem that I have is that, when I first click submit, no validation takes place and an empty form also gets submitted.
From the second time on wards, each time I click submit the validation check kicks in and reports an empty field.
My code:
<form>
<label for="id">ID:</label>
<input type="text" class="form-control" id="id" required>
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" required />
<button type="submit" class="btn btn-info pull-right" id="sub">Submit</button>
</form>
POST request:
$("#sub").click(function(){
var sendf = {
id: $("#id").val(),
name: $("#name").val()
};
$.ajax({
type: "POST",
url: "",
data: sendf,
dataType: "text",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
},
success: function(response, status, xhr) {
},
}); //ajax ends
event.preventDefault();
$(this).unbind();
});//sub click ends
Am I doing anything wrong? On button click, its just a simple POST request that takes place. Any help is appreciated. Thanks !