Skip to content

Instantly share code, notes, and snippets.

@SomeNiceCode
Created July 1, 2025 20:00
Show Gist options
  • Save SomeNiceCode/624f14f9c9737b4993879d852c32f7f6 to your computer and use it in GitHub Desktop.
Save SomeNiceCode/624f14f9c9737b4993879d852c32f7f6 to your computer and use it in GitHub Desktop.
Forms Validation JS
document.getElementById("registrationForm").addEventListener("submit", function (e) {
e.preventDefault();
const email = document.getElementById("email").value.trim();
const username = document.getElementById("username").value.trim();
const password = document.getElementById("password").value;
const errorMsg = document.getElementById("errorMsg");
errorMsg.textContent = "";
if (!email.includes("@")) {
errorMsg.textContent = "Введите корректный email.";
return;
}
if (username.length < 3) {
errorMsg.textContent = "Логин должен содержать минимум 3 символа.";
return;
}
if (password.length < 6) {
errorMsg.textContent = "Пароль должен быть не менее 6 символов.";
return;
}
alert("Форма успешно отправлена!");
//можно отправить данные на сервер
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment