Skip to content

Commit

Permalink
add Jquery Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
IsmailM committed May 17, 2017
1 parent 57e8aff commit 996e83e
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 152 deletions.
263 changes: 114 additions & 149 deletions static/assets/js/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,192 +13,157 @@ if (!PP) {

// PP module
(function() {
PP.equalizeAboutBoxes = function() {
PP.equalSizeDiv('.collection');
$(window).resize(function() {
$('.collection').css('height', 'auto');
PP.equalSizeDiv('.collection');
PP.initLoginForm = function() {
$('#login_modal').modal({ complete: function() { PP.showLoginForm(); } }); // Init Modals and ensure
PP.loginValidation();
// Submit form upon clicking the login button in the login modal
$('#login_btn').on('click', function() {
$('#login_form').submit();
});
};

PP.equalSizeDiv = function(div) {
if ($(div).length === 0) return;
var maxHeight = 0;
$(div).each(function() {
if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
// submit form upon pressing enter in the login form
$('#login_form input').keydown(function(e) {
if (e.keyCode == 13) $('#login_form').submit();
});
$(div).css('height', maxHeight);
// Open open Login Model On Clicking #enter_login_btn
$('#enter_login_btn').on('click', function() {
PP.showLoginForm();
});
PP.demoAutomaticLogin();
};

PP.initLogin = function() {
PP.openLoginModelOnClick('#enter_login_btn');
PP.loginValidation = function() {
$('#login_form').validate({
rules: {
name: {
required: true
},
password: {
required: true
},
},
submitHandler: function(form) {
$('#auth_modal').modal({ dismissible: false, endingTop: '20%' });
$('#auth_modal').modal('open');

// Submit form upon clicking the login button in the modal
$('#login_btn').on('click', function() {
PP.submitLogin();
$.ajax({
type: 'POST',
url: '/login',
data: $('#login_form').serialize(),
dataType: 'json',
timeout: 120000,
success: function(data) {
window.location.href = '/search';
},
error: function(xhr, msg) {
$('#auth_modal').modal('close');
$("#username, #password").addClass("invalid");
$("#username, #password").prop("aria-invalid", "true");
$('#login_form_error_msg').show();
}
});
}
});
};

// submit form upon pressing enter in the login form
$('#login_form input').keydown(function(e) {
if (e.keyCode == 13) PP.submitLogin();
});
//
PP.showLoginForm = function() {
$('#login_content').show();
$('#register_btn').show();
$('#login_btn').show();
$('#demo_login_btn').show();

// Automatically login when selecting the demo login button
$('#register_content').hide();
$('#enter_login_btn').hide();
$('#submit_register_btn').hide();
};

// Automatically login when selecting the demo login button
PP.demoAutomaticLogin = function() {
$('#demo_login_btn, #parallax_demo_login_btn').on('click', function() {
$('#name').val('demo');
$('#password').val('demo123');
PP.submitLogin();
});

$('#register_btn').on('click', function() {
PP.showRegistrationForm();
$('#login_form').submit();
});
};

$('#change_password_btn').on('click', function () {
PP.showChangePasswordForm();
});
PP.initRegistrationForm = function() {
PP.registrationValidation();
PP.showRegistrationFormOnClick();

$('#submit_register_btn').on('click', function() {
PP.submitRegisterForm();
});

$('#submit_change_password_btn').on('click', function () {
PP.submitChangePassword();
$('#register_form').submit();
});
};

// TODO: Replace with JQuery.validate
// show Login Model
PP.submitLogin = function() {
$('#auth_modal').modal({ dismissible: false, endingTop: '20%' });
$('#auth_modal').modal('open');

$.ajax({
type: 'POST',
url: '/login',
data: $('#login_form').serialize(),
dataType: 'json',
timeout: 120000,
success: function(data) {
window.location.href = '/search';
PP.registrationValidation = function() {
$('#register_form').validate({
rules: {
name: {
required: true
},
email: {
required: true
},
affliation: {
required: true
},
"group[]": {
required: true
}
},
error: function(data, msg) {
$('#auth_modal').modal('close');
$("#username, #password").addClass("invalid");
$("#username, #password").prop("aria-invalid", "true");
$('#login_form_error_msg').show();
submitHandler: function(form) {
$.ajax({
type: 'POST',
url: '/register',
data: $('#register_form').serialize(),
dataType: 'json',
timeout: 120000,
success: function(data) {
$('#registering_msg_success').text(data.message);
$('#registration_error').hide();
$('#registration_success').show();
},
error: function(xhr, msg) {
$('#registering_msg_error').text(xhr.responseJSON.message);
$('#registration_success').hide();
$('#registration_error').show();
}
});
}
});
};

//
PP.showRegistrationForm = function() {
$('#login_content').hide();
$('#change_password_content').hide();
$('#change_password_btn').hide();
$('#register_btn').hide();
$('#submit_change_password_btn').hide();

$('#login_btn').hide();
$('#demo_login_btn').hide();

$('#register_content').show();
$('#enter_login_btn').show();
$('#submit_register_btn').show();
};

//
PP.showChangePasswordForm = function () {
$('#change_password_form')[0].reset();
$("#change_password_form_error_msg").hide();

PP.showRegistrationFormOnClick = function() {
$('#register_btn').on('click', function() {
$('#login_content').hide();
$('#change_password_btn').hide();
$('#register_btn').hide();
$('#change_password_successful').hide();

$('#login_btn').hide();
$('#demo_login_btn').hide();

$('#register_content').hide();
$('#submit_register_btn').hide();

$('#register_content').show();
$('#enter_login_btn').show();
$('#change_password_content').show();
$('#submit_change_password_btn').show();

};

//
PP.showLoginForm = function() {
$('#login_content').show();
$('#change_password_btn').show();
$('#register_btn').show();
$('#login_btn').show();
$('#demo_login_btn').show();

$('#register_content').hide();
$('#change_password_content').hide();
$('#enter_login_btn').hide();
$('#submit_register_btn').hide();
$('#submit_change_password_btn').hide();
};

//
PP.openLoginModelOnClick = function(id) {
$(id).on('click', function() {
PP.showLoginForm();
$('#submit_register_btn').show();
});
};

//
PP.submitRegisterForm = function() {
$.ajax({
type: 'POST',
url: '/register',
data: $('#register_form').serialize(),
dataType: 'json',
timeout: 120000,
success: function(data) {
$('#registering_msg_success').text(data.message);
$('#registration_error').hide();
$('#registration_success').show();
},
error: function(data, msg) {
$('#registering_msg_error').text(data.responseJSON.message);
$('#registration_success').hide();
$('#registration_error').show();
console.log(data);
console.log(msg);
}
// make the profile boxes on the about page the same height
PP.equalizeAboutBoxes = function() {
PP.equalizeDivHeight('.collection');
$(window).resize(function() {
$('.collection').css('height', 'auto');
PP.equalizeDivHeight('.collection');
});
};

PP.submitChangePassword = function () {
$('#auth_modal').modal({ dismissible: false, endingTop: '20%' });
$('#auth_modal').modal('open');
$('#change_password_form_error_msg').hide();
$('#change_password_successful').hide();

$.ajax({
type: 'POST',
url: '/change_password',
data: $('#change_password_form').serialize(),
dataType: 'json',
timeout: 120000,
success: function (data) {
$('#auth_modal').modal('close');
$('#change_password_successful').show();
$("#change_password_successful").text(data.success);
},
error: function (data, msg) {
$('#auth_modal').modal('close');
$("#username, #password, #new_password_1, #new_password_2").addClass("invalid");
$("#username, #password, #new_password_1, #new_password_2").prop("aria-invalid", "true");
$('#change_password_form_error_msg').show();
$("#change_password_form_error_msg").text(data.responseJSON.error);
}
});
PP.equalizeDivHeight = function(div) {
if ($(div).length === 0) return;
var maxHeight = 0;
$(div).each(function() {
if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
});
$(div).css('height', maxHeight);
};
}());
// End of PP module
80 changes: 80 additions & 0 deletions static/assets/js/phenopolis.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,86 @@ if (!PP) {
});
};

PP.setUpValidatorDefaults = function() {
$.validator.addMethod("secure_password", function(value, element) {
return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value) && // consists of only these
/[a-z]/.test(value) && // has a lowercase letter
/[A-Z]/.test(value) && // has an uppercase letter
/\d/.test(value); // has a digit
}, 'Password must contain an upper case letter, a lower case letter and a digit.');

$.validator.setDefaults({
errorClass: 'invalid',
validClass: "valid",
errorPlacement: function (error, element) {
$(element)
.closest("form")
.find("label[for='" + element.attr("id") + "']")
.attr('data-error', error.text());
},
});
};

PP.initChangePasswordForm = function() {
PP.changePasswordValidation();

$('#submit_change_password_btn').on('click', function() {
$('#change_password_form').submit();
});
};

PP.changePasswordValidation = function() {
$('#change_password_form').validate({
rules: {
current_password: {
required: true,
},
new_password_1: {
required: true,
secure_password: true,
minlength: 6
},
new_password_2: {
required: true,
secure_password: true,
minlength: 6,
equalTo: '#new_password_1'
},
},
messages: {
new_password_2: {
equalTo: 'Both new passwords must match.'
}
}
submitHandler: function(form) {
$('#auth_modal').modal({ dismissible: false, endingTop: '20%' });
$('#auth_modal').modal('open');
$('#change_password_form_error_msg').hide();
$('#change_password_successful').hide();

$.ajax({
type: 'POST',
url: '/change_password',
data: $('#change_password_form').serialize(),
dataType: 'json',
timeout: 120000,
success: function (data) {
$('#auth_modal').modal('close');
$('#change_password_successful').show();
$("#change_password_successful").text(data.success);
},
error: function (xhr, msg) {
$('#auth_modal').modal('close');
$("#username, #password, #new_password_1, #new_password_2").addClass("invalid");
$("#username, #password, #new_password_1, #new_password_2").prop("aria-invalid", "true");
$('#change_password_form_error_msg').show();
$("#change_password_form_error_msg").text(data.responseJSON.error);
}
});
}
});
};

/*
* Generic Functions
*/
Expand Down
Loading

0 comments on commit 996e83e

Please sign in to comment.