I've made a few functions to check form input for an AJAX request. I am still getting used to JavaScript and some of these AJAX requests. I am looking for any suggestions to better work with JavaScript functions, and passing variables through a function level scoping.
//Form Handler
$("#form-submit").submit(function(e) {
e.preventDefault();
$.ajax({
beforeSend: function(){
return (checkAll());
return (passwordCheck());
},
complete: function(){
},
type: "POST",
url: '',
data: $("#form-submit").serialize(),
success: function(data)
{
//window.location = '/dashboard.php';
// Set Session Php Etc
alert('php ran');
return true;
}
});
});
//Pushing all Input Values to an Array
function checkAll() {
var arr = [];
$('#form-submit :input').each(function() {
arr.push($(this).val());
});
return (checkArray(arr));
}
//Checks Array for empty strings
function checkArray(arr){
for(var i=0; i < arr.length; i++) {
console.log(arr[i]);
if (arr[i].trim() == '') {
alert('Please Enter All Fields');
return false;
} else {
return true;
}
}
}
//Matches Password
function passwordCheck() {
var pass1 = $('#password').val();
var pass2 = $('#password-check').val();
if(pass1 != pass2) {
$('#password').addClass('highlight');
$('#password-check').addClass('highlight');
alert("Passwords don't match");
return false;
} else {
$('#password').removeClass('highlight');
$('#password-check').removeClass('highlight');
return true;
}
}
beforeSend
, you have tworeturn
statements. \$\endgroup\$ – hjpotter92 Sep 6 '14 at 8:35return (passwordCheck() && checkAll());
? \$\endgroup\$ – hjpotter92 Sep 6 '14 at 8:46