<script type="text/javascript">
$(document).ready(function(){
$('#addprojectform').live('submit', function (e) {
e.preventDefault();
$(".error").remove();
var startdate = $("#startdate").val();
var client = $('#client').val();
var source_lang= $('#source_lang option:selected').length;
var targ_lang = $('#targ_lang option:selected').length;
var qty = $('#qty').val();
var hasError = false;
if(startdate == '') {
$("#startdate").after('<span class="error" style="color: red;">This field is required.</span>');
hasError = true;
}
if(client == '') {
$("#client").after('<span class="error" style="color: red;">This field is required. </span>');
hasError = true;
}
if(!(source_lang)) {
$("#source_lang").after('<span class="error" style="color: red;">This field is required.</span>');
hasError = true;
}
if(!(targ_lang)) {
$("#targ_lang").after('<span class="error" style="color: red;">This field is required.</span>');
hasError = true;
}
if(qty == '') {
$("#qty").after('<span class="error" style="color: red;">This field is required.</span>');
hasError = true;
}
} // if translation
if(hasError == false)
return true;
});
});
</script>
I had this code. I want to validate the form before submission. I can’t control when the form has no errors. I want to submit the form if there are no errors found. I added this code:
if(hasError == false)
return true;
but doesn’t submit the form.. Please anyone can help?
remove
and change
into
as a side note,
live()is deprecated in favour ofon()on newer jQuery version