jquery - Form validation with click event -
i have form validation:
<form role="form" method="post" action="" > <input type="password" class="form-control top-buffer-sm" placeholder="password" name="password" required autofocus></input> <input type="email" class="form-control top-buffer-sm" placeholder="email" name="email" required autofocus></input> <div class="modal-footer"> <button id="createuser" type="submit" class="btn btn-primary">create</button> <button type="button" class="btn btn-default" data-dismiss="modal">close</button> </div>
my submit button has id , need other functionality before submit form:
$('#createuser').click(function(){ //check see if validation passed //do custom stuff //now make ajax call $.ajax(); });
but if form not valid, ajax call still being made, of course resulting in server error. how check see if form passed validation when click submit button? thanks
why not list submit event on form?
<form role="form" method="post" action="" id="myform">
then:
$('#myform').on('submit', function(event) { // prevent form submission on click event.preventdefault(); // write code determins if form valid. // `this` refers form. if (formisvalidcondition) { // ajax call here } else { // return response user return false; } });
Comments
Post a Comment