Witam.
Chciałbym, żeby po przejściu walidacji formularza, po wciśnięciu na input[type=submit], wyświetlił się alert o poprawności wypełnienia i przekierowaniu na podstronę np. index.php
Problem jest taki, że walidacja działa, ale nie następuje przekierowanie na stronę index.php ;/
Kod html:
<form method="POST" action="index.php" id="register-form">
<input name="user_name" type="text" placeholder="Your login..." id="loginInput"/>
<input name="user_passwd" type="password" placeholder="Your password..." id="passwordInput" />
<input name="user_email" type="text" placeholder="Your e-mail..." id="emailInput"/>
<input name="user_phone"type="text" placeholder="Your phone number..." id="phoneNumberInput" />
<input name="user_city" type="text" placeholder="Your city name..." id="cityInput" />
<input type="submit" name="register" class="submit-btn" value="sign up">
</form>
Kod js:
const loginField = document.getElementById("loginInput");
const passwordField = document.getElementById("passwordInput");
const emailField = document.getElementById("emailInput");
const cityField = document.getElementById("cityInput");
const phoneNumberField = document.getElementById("phoneNumberInput");
const form = document.getElementById("register-form");
const signupBtn = document.querySelector("input.submit-btn");
form.onsubmit = function validateForm(e) {
e.preventDefault();
if (loginField.value === "" || loginField.value.length < 3) {
window.alert("Fill login field properly ! Your login must have more than 3 characters!");
loginField.focus();
return false;
}
if (passwordField.value === "" || passwordField.value.length < 3) {
window.alert("Fill password field properly ! Your password must have more than 8 characters!");
passwordField.focus();
return false;
}
if (emailField.value.indexOf("@") == -1 || emailField.value.length < 6) {
window.alert("Fill email field properly !");
emailField.focus();
return false;
}
if (isNaN(phoneNumberField.value) || phoneNumberField.value.length < 9) {
window.alert("Fill Your phone number corectly !");
phoneNumberField.focus();
return false;
}
if (cityField.value === "") {
window.alert("Fill city name field properly ! ");
loginField.focus();
return false;
}
alert("Form submitted. You can now sign in too Your account !");
return true;
}
signupBtn.addEventListener("click", validateForm);
Proszę o jakąś wskazówkę, bo jestem już nieźle poirytowany.