The problem in the code below resides in the if...else statement. If a user tries to create an account with a username or email already present in localStorage the program displays an error message. This works fine however if the if statement is false the page reloads before reaching the else statement. I have used console.log to check whether the else statement is run however as stated above the page reloads before ever reaching the else statement. I have also tried putting event.preventDefault in the if statement however that doesnt work either. Any help would be greatly appreciated. Thanks in advance.
Edit: the function is called using onclick in html when the button is pressed.
function storeRegistrationInfo(){
let person = {};
person.fullname = document.getElementById("regFullName").value;
person.username = document.getElementById("regUserName").value;
let username = document.getElementById("regUserName").value;
person.email = document.getElementById("regEmail").value;
person.password = document.getElementById("regPassword").value;
person.repeatedPassword = document.getElementById("repeatPassword").value;
let per = JSON.parse(localStorage.getItem(username));
if (person.username === per.username || person.email === per.email){
document.getElementById("signUpStatus").innerText = "Username or Email already taken!";
}else {
localStorage[person.username] = JSON.stringify(person);
}
event.preventDefault();
}
Aucun commentaire:
Enregistrer un commentaire