Есть форма
https://github.com/iWyse/LoginForm
Хотелось бы чтобы при нажатии на Enter срабатывала именно кнопка с синим фоном, я попробовал засунуть все в такой код
formAuth.addEventListener('submit', (e) {
if ( кнопка регистрации видна ) {
}
if ( кнопка авторизации видна ) {
}
if ( кнопка логаута видна ) {
}
})
Но при нажатии на enter срабатывает все сразу, как быть ?
Вот код на данный момент
//* Ивент регистрации
// let validRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
let validRegex =
/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
let errorValidate = "Invalid email or password";
signUp.addEventListener("click", (e) => {
let email = document.getElementById("email").value;
let password = document.getElementById("password").value;
checkPassword(password);
function checkPassword(password) {
const beginWithoutDigit = /^\D.*$/;
const withoutSpecialChars = /^[^-() /]*$/;
const containsLetters = /^.*[a-zA-Z]+.*$/;
const minimum8Chars = /^.{8,}$/;
const withoutSpaces = /^[\S]$/;
if (
beginWithoutDigit.test(password) &&
withoutSpecialChars.test(password) &&
containsLetters.test(password) &&
minimum8Chars.test(password) &&
withoutSpaces.test(password)
) {
console.log(password);
} else {
// modalVisible(errorValidate, isError());
console.log(password + " error");
}
}
// let password = document.getElementById("password").value;
//* Валидация EMAIL
if (email.match(validRegex)) {
createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user;
set(ref(database, "users/" + user.uid), {
email: email,
password: password,
}),
modalVisible("Created " + email, isSuccess());
accountIsRegistred();
document.getElementById("password").value = "";
})
.catch((error) => {
let errorCode = error.code;
let errorMessage = error.message;
if (errorCode == "auth/email-already-in-use") {
errorMessage = "Email already in use";
} else if (errorCode == "auth/weak-password") {
errorMessage = "Password should be at least 6 characters";
} else if (errorCode == "auth/internal-error") {
errorMessage = errorValidate;
} else if (errorCode == "auth/invalid-email") {
errorMessage = "Invalid email or password";
} else if (errorCode == "auth/missing-email") {
errorMessage = "Missing email";
} else {
error.code;
}
modalVisible(errorMessage, isError());
});
} else {
modalVisible(errorValidate, isError());
}
});
//* Ивент авторизации
login.addEventListener("click", (e) => {
let email = document.getElementById("email").value;
let password = document.getElementById("password").value;
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user;
const dt = new Date();
update(ref(database, "users/" + user.uid), {
last_login: dt,
});
modalVisible("Loged in! " + email, isSuccess());
logged();
createEmails();
loggedTitle.innerHTML = ` You are logged as <b>${email}</b>`;
})
.catch((error) => {
let errorCode = error.code;
let errorMessage = error.message;
console.log(errorCode);
console.log(errorMessage);
if (errorCode == "auth/user-not-found") {
errorMessage = "User not found";
} else if (errorCode == "auth/weak-password") {
errorMessage = "Password should be at least 6 characters";
} else if (errorCode == "auth/internal-error") {
errorMessage = errorValidate;
} else if (errorCode == "auth/invalid-email") {
errorMessage = "Invalid email or password";
} else if (errorCode == "auth/missing-email") {
errorMessage = "Missing email";
} else if (errorCode == "auth/wrong-password") {
errorMessage = "Wrong password";
} else if (errorCode == "auth/too-many-requests") {
errorMessage = "Too many requests";
} else {
error.code;
}
modalVisible(errorMessage, isError());
});
});
//* Ивент выхода из авторизации
logout.addEventListener("click", (e) => {
signOut(auth)
.then(() => {
modalVisible("logged out", isSuccess()); //* Вторым параметром на алерт вышаю либо ошибку, либо "чек" если все хорошо
goLogin();
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
modalVisible(errorMessage, isError());
});
});