I have created a registration form using jQuery. I am validating username and password using an AJAX request. The "submit" button is originally disabled and is only enabled when all three fields of username, email and password are filled and validated.
How and what can I improve in this code?
$(document).ready(function() {
var usernameFlag = 1, emailFlag = 1, passwordFlag = 1;
//event handler on input for username
document.getElementById("reg_username").addEventListener("input", function() {
var username = $("#reg_username").val();
if(username !== null && username !== "") {
usernameCheck(username).done(usernameCallback);
}
else {
usernameFlag = 1;
$("#tick_username").hide();
$("#cross_username").hide();
enableButton();
}
});
//event handler on input for email
document.getElementById("reg_email").addEventListener("input", function () {
var email = $("#reg_email").val();
if(email !== null && email !== "") {
emailCheck(email).done(emailCallback);
}
else {
emailFlag = 1;
$("#tick_email").hide();
$("#cross_email").hide();
enableButton();
}
});
//event handler on input for password
document.getElementById("reg_password").addEventListener("input", function () {
var password = $("#reg_password").val();
if (password !== null && password !== "") {
passwordFlag = 0;
enableButton();
}
else {
passwordFlag = 1;
enableButton();
}
});
function usernameCallback(response) {
var usernameResponse = parseInt(response);
usernameFlag = usernameResponse;
if(usernameResponse === 1) {
$("#tick_username").hide();
$("#cross_username").fadeIn();
}
else {
$("#cross_username").hide();
$("#tick_username").fadeIn();
}
enableButton();
}
function emailCallback(response) {
var emailResponse = parseInt(response);
emailFlag = emailResponse;
if(emailResponse === 1) {
$("#tick_email").hide();
$("#cross_email").fadeIn();
}
else {
$("#cross_email").hide();
$("#tick_email").fadeIn();
}
enableButton();
}
//ajax call to validate username
function usernameCheck (username) {
return $.ajax({
type: "POST",
url: "check_availability.php",
data: "username="+username
});
}
//ajax call to validate email
function emailCheck (email) {
return $.ajax({
type: "POST",
url: "check_availability.php",
data: "email="+email
});
}
//function to enable submit button
function enableButton () {
if (usernameFlag === 0 && emailFlag === 0 && passwordFlag === 0) {
$("#register_btn").prop("disabled", false);
}
else {
$("#register_btn").prop("disabled", true);
}
}
});
I am using three extra variables to keep track of the fields. The flags are updated if the user changes a value. After every change in input field, enableButton()
checks the flags and enables the button if all of them have a value of 0.