In jQuery, I have been reluctant to create a better form validation, and I feel the time has come for me to do so.
With that said, I have been using this particular type of form validation, and I would like to simplify it.
$('#saveSubmit').on('click', function()
{
var criteria =
{
company: $('#company').val(),
partnercode: $('#partnercode').val(),
office: $('#office').val(),
// few more
}
// form validation starts here
if(criteria.company == "")
{
$('#compError').show();
return false;
}
if(criteria.partnercode == "")
{
$('#compError').show();
return false;
}
if(criteria.office == "")
{
$('#compError').show();
return false;
}
// few more if statements
else
{
// if the form parameters are good, send to processing script
$.post('process/editUser.php', {criteria:criteria}, function(data)
{
if(data.indexOf("Error") >= 0)
{
$('#errorModal').modal('show');
$('#message').text(data);
return false;
}
else
{
$('#successModal').modal('show');
$('#messageSuccess').text(data);
$('#successModal').on('hidden.bs.modal', function()
{
$('#successModal').modal('hide');
location.reload();
});
}
});
}
});
All of this works like how it should, but I want to make it less verbose, thus the need for a function.
I want to replace that huge if
/else
statement within that click event with a function.