I inherited some code from a website developer that has since become impossible to contact and we have one issue with the site, the password reset function on the login screen.
This is from the index.js file that handles the login and forgotten password functions. The login function (not included) works fine. When entering and submitting a valid email address in our database, it doesnt seem to ever actually forward the request, instead it looks like it is, but then outputs the line “Opps!Check your internet and please try again later!”
Uses the sweetalert js framework for alerts
Original:
function forgotPass(){
swal({ title: "Forgot Password?!",
text: "Please enter your registered E-mail:",
type: "input", showCancelButton: true,
closeOnConfirm: false, animation: "slide-from-top",
inputPlaceholder: "Your registered E-mail" },
function(inputValue){
if (inputValue === false)
return false;
if (inputValue === ""){
swal.showInputError("You need to enter E-mail!");
return false }
var recovery_email={"recovery_email":inputValue};
$(".sweet-alert").addClass("hideSweet");
$(".sweet-overlay").addClass("hideSweet");
HoldOn.open({
theme:"sk-fading-circle",
message:"<h4>Processing your information! Please Wait...</h4>"
});
/*var delay=2000; //1 seconds
setTimeout(function(){
//your code to be executed after 1 seconds
HoldOn.open({
theme:"sk-fading-circle",
message:"<h4>Sending Verification code to your registered e-mail! Please Wait...</h4>"
});
}, delay); */
$.ajax({
url: "ajax/ajax_forgot_password.php",
type: "POST",
data: recovery_email,
success: function (data, textStatus, jqXHR) {
HoldOn.close();
$(".sweet-alert").removeClass("hideSweet");
if(data.trim()=="success"){
var email=inputValue;
swal({
title: "Verification",
text: "The verification code has been sent to your registered mail id "+inputValue+".Please enter the verification code:",
type: "input", showCancelButton: true,
closeOnConfirm: false, animation: "slide-from-top",
inputPlaceholder: "Enter the verification code" },
function(inputValue){
if (inputValue === false)
return false;
if (inputValue === "") {
swal.showInputError("You need to enter the verification code!");
return false }
var verification={"verification_code":inputValue,"email":email};
var verification={"verification":verification};
$(".sweet-alert").addClass("hideSweet");
HoldOn.open({
theme:"sk-fading-circle",
message:"<h4>Verification pending!!!Please Wait...</h4>"
});
$.ajax({
url: "ajax/ajax_forgot_password.php",
type: "POST",
data: verification,
success: function (data, textStatus, jqXHR) {
$(".sweet-alert").removeClass("hideSweet");
HoldOn.close();
if(data.trim()=="success"){
function PasswordInput(){
swal({
title: "Change Password",
text: "Please enter new password:",
type: "input",
inputType: "password",
inputPlaceholder: "Enter new password",
showCancelButton: true,
closeOnConfirm: false, animation: "slide-from-top"
},
function (inputValue){
if (inputValue === false)
return false;
if (inputValue === "") {
swal.showInputError("You need to enter new password!");
return false }
var password1=inputValue;
swal({
title: "Re-Type your Password!", text: "Re-Type your password:",
type: "input",
inputType: "password", showCancelButton: true,
closeOnConfirm: false, animation: "slide-from-top",
inputPlaceholder: "Re-Type your password" },
function(inputValue){
if (inputValue === false) return false;
if (inputValue === "") {
swal.showInputError("You need to re-type your password!");
return false }else if(inputValue==password1){
var password_details={"new_password":inputValue,"email":email};
var password_details={"password_details":password_details};
$(".sweet-alert").addClass("hideSweet");
$.ajax({
url: "ajax/ajax_forgot_password.php",
type: "POST",
data: password_details,
success: function (data, textStatus, jqXHR) {
$(".sweet-alert").removeClass("hideSweet");
if(data.trim()=="success"){
swal({
title: "Success!",
text: "Your password has been changed successfully!",
type: "success",
confirmButtonClass: "btn-success",
confirmButtonText: "Done!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
window.open("index.php","_self");
}
});
}else if(data.trim()=="error"){
swal.showInputError("Opps!Please try again later!");
return false
}else{
swal.showInputError("Opps!Please try again later!");
return false
}
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
}else{
swal.showInputError("Passwords doesn't match!<br>Redirecting to <strong> enter new password </Strong> in 10 seconds..");
//var error="Passwords doesn't match! Please try again";
var delay=10000; //2 seconds
setTimeout(function(){
//your code to be executed after 1 seconds
PasswordInput();
}, delay);
//exit;
}
});
});
}
PasswordInput();
}else if(data.trim()=="error"){
swal.showInputError("Verification code doesn't match.Please try again!");
return false
}else{
swal.showInputError("Opps!Please try again later!");
return false
}
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
});
}else if(data.trim()=="not_exists"){
swal.showInputError("Entered E-mail ID is not exists!");
return false
}else{
swal.showInputError("Opps!Check your internet and please try again later!");
return false
}
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
});
}
“Corrected” via validator:
function forgotPass() {
swal({
title: 'Forgot Password?!',
text: 'Please enter your registered E-mail:',
type: 'input',
showCancelButton: true,
closeOnConfirm: false,
animation: 'slide-from-top',
inputPlaceholder: 'Your registered E-mail',
},
(inputValue) => {
if (inputValue === false) return false;
if (inputValue === '') {
swal.showInputError('You need to enter E-mail!');
return false;
}
var recovery_email = { recovery_email: inputValue };
$('.sweet-alert').addClass('hideSweet');
$('.sweet-overlay').addClass('hideSweet');
HoldOn.open({
theme: 'sk-fading-circle',
message: '<h4>Processing your information! Please Wait...</h4>',
});
/* var delay=2000; //1 seconds
setTimeout(function(){
//your code to be executed after 1 seconds
HoldOn.open({
theme:"sk-fading-circle",
message:"<h4>Sending Verification code to your registered e-mail! Please Wait...</h4>"
});
}, delay); */
$.ajax({
url: 'ajax/ajax_forgot_password.php',
type: 'POST',
data: recovery_email,
success(data, textStatus, jqXHR) {
HoldOn.close();
$('.sweet-alert').removeClass('hideSweet');
if (data.trim() === 'success') {
var email = inputValue;
swal({
title: 'Verification',
text: `The verification code has been sent to your registered mail id ${inputValue}.Please enter the verification code:`,
type: 'input',
showCancelButton: true,
closeOnConfirm: false,
animation: 'slide-from-top',
inputPlaceholder: 'Enter the verification code',
},
(inputValue) => {
if (inputValue === false) return false;
if (inputValue === '') {
swal.showInputError('You need to enter the verification code!');
return false;
}
var verification = { verification_code: inputValue, email };
var verification = { verification };
$('.sweet-alert').addClass('hideSweet');
HoldOn.open({
theme: 'sk-fading-circle',
message: '<h4>Verification pending!!!Please Wait...</h4>',
});
$.ajax({
url: 'ajax/ajax_forgot_password.php',
type: 'POST',
data: verification,
success(data, textStatus, jqXHR) {
$('.sweet-alert').removeClass('hideSweet');
HoldOn.close();
if (data.trim() === 'success') {
function PasswordInput() {
swal({
title: 'Change Password',
text: 'Please enter new password:',
type: 'input',
inputType: 'password',
inputPlaceholder: 'Enter new password',
showCancelButton: true,
closeOnConfirm: false,
animation: 'slide-from-top',
},
(inputValue) => {
if (inputValue === false) return false;
if (inputValue === '') {
swal.showInputError('You need to enter new password!');
return false;
}
var password1 = inputValue;
swal({
title: 'Re-Type your Password!',
text: 'Re-Type your password:',
type: 'input',
inputType: 'password',
showCancelButton: true,
closeOnConfirm: false,
animation: 'slide-from-top',
inputPlaceholder: 'Re-Type your password',
},
(inputValue) => {
if (inputValue === false) return false;
if (inputValue === '') {
swal.showInputError('You need to re-type your password!');
return false;
} if (inputValue === password1) {
var password_details = { new_password: inputValue, email };
var password_details = { password_details };
$('.sweet-alert').addClass('hideSweet');
$.ajax({
url: 'ajax/ajax_forgot_password.php',
type: 'POST',
data: password_details,
success(data, textStatus, jqXHR) {
$('.sweet-alert').removeClass('hideSweet');
if (data.trim() === 'success') {
swal({
title: 'Success!',
text: 'Your password has been changed successfully!',
type: 'success',
confirmButtonClass: 'btn-success',
confirmButtonText: 'Done!',
closeOnConfirm: false,
closeOnCancel: false,
},
(isConfirm) => {
if (isConfirm) {
window.open('index.php', '_self');
}
});
} else if (data.trim() === 'error') {
swal.showInputError('Opps!Please try again later!');
return false;
} else {
swal.showInputError('Opps!Please try again later!');
return false;
}
},
error(jqXHR, textStatus, errorThrown) {
},
});
} else {
swal.showInputError("Passwords doesn't match!<br>Redirecting to <strong> enter new password </Strong> in 10 seconds..");
// var error="Passwords doesn't match! Please try again";
var delay = 10000; // 2 seconds
setTimeout(() => {
// your code to be executed after 1 seconds
PasswordInput();
}, delay);
// exit;
}
});
});
}
PasswordInput();
} else if (data.trim() === 'error') {
swal.showInputError("Verification code doesn't match.Please try again!");
return false;
} else {
swal.showInputError('Opps!Please try again later!');
return false;
}
},
error(jqXHR, textStatus, errorThrown) {
},
});
});
} else if (data.trim() === 'not_exists') {
swal.showInputError('Entered E-mail ID is not exists!');
return false;
} else {
swal.showInputError('Opps!Check your internet and please try again later!');
return false;
}
},
error(jqXHR, textStatus, errorThrown) {
},
});
});
}
I tried several JS validators and “corrected”.


