Skip to content

Instantly share code, notes, and snippets.

@mmgj
Last active November 22, 2019 15:14
Show Gist options
  • Save mmgj/8bed8664f31bdcb61954725f515ead13 to your computer and use it in GitHub Desktop.
Save mmgj/8bed8664f31bdcb61954725f515ead13 to your computer and use it in GitHub Desktop.
const name = document.querySelector('.name');
const email = document.querySelector('.email');
const error = document.querySelector('.error');
const firstpassword = document.querySelector('.password1');
const secondpassword = document.querySelector('.password2');
const textName = document.querySelector('.nameText');
const textEmail = document.querySelector('.emailText');
const textPassword = document.querySelector('.passwordText');
const button = document.querySelector('button');
button.addEventListener('click', validateForm);
function validateForm() {
let formValidated = true;
[textName, textEmail, textPassword].forEach(errorText => {
errorText.innerText = '';
});
if (name.value.length < 3) {
textName.innerText = 'Name must be at least 3 characters long...';
formValidated = false;
return;
}
if (email.value.indexOf('@') == -1 || email.value.indexOf('.') == -1) {
textEmail.innerText = 'Insert a valid email..';
formValidated = false;
return;
}
if (firstpassword.value <= 0) {
textPassword.innerText = 'Enter a password, joker.';
formValidated = false;
return;
}
if (firstpassword.value !== secondpassword.value) {
textPassword.innerText = 'Passwords must match...';
formValidated = false;
return;
}
if (formValidated) {
window.confirm('Registration successful! A win is you!');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment