Home > Back-end >  Show '.signUp' class when <a>Sign up here</a> clicked and hide when <a>L
Show '.signUp' class when <a>Sign up here</a> clicked and hide when <a>L

Time:11-30

I have two classes 'signIn' and 'singUp'. So I need switch between them.

const form = document.getElementById('form');
const username = document.getElementById('username');
const email = document.getElementById('email');
const password = document.getElementById('password');
const password2 = document.getElementById('password2');

form.addEventListener('submit', (e) => {
  e.preventDefault();
  checkInputs();
});

function checkInputs() {
  const usernameValue = username.value.trim();
  const emailValue = email.value.trim();
  const passwordValue = password.value.trim();
  const password2Value = password2.value.trim();

  if (usernameValue === '') {
    setErrorFor(username, 'Username cannot be blank');
  } else {
    setSuccessFor(username);
  }

  if (emailValue === '') {
    setErrorFor(email, 'Email cannot be blank');
  } else if (!isEmail(emailValue)) {
    setErrorFor(email, 'Email is not valid');
  } else {
    setSuccessFor(email);
  }

  if (passwordValue === '') {
    setErrorFor(password, 'Password cannot be blank');
  } else if (!isPassword(passwordValue)) {
    setErrorFor(password, 'Password is not valid')
  } else {
    setSuccessFor(password);
  }

  if (password2Value === '') {
    setErrorFor(password2, 'Password2 cannot be blank');
  } else if (!isPassword(password2Value)) {
    setErrorFor(password2, 'Password is not valid');
  } else if (passwordValue !== password2Value) {
    setErrorFor(password2, 'Passwords does not match');
  } else {
    setSuccessFor(password2);
  }


}

function setErrorFor(input, message) {
  const formControl = input.parentElement;
  const small = formControl.querySelector('small');

  small.innerText = message;

  formControl.className = 'form-control error';
}

function setSuccessFor(input) {
  const formControl = input.parentElement;
  formControl.className = 'form-control success';
}

function isEmail(email) {
  return /^[a-zA-Z0-9.!#$%&'* /=?^_`{|}~-] @[a-zA-Z0-9-] (?:\.[a-zA-Z0-9-] )*$/.test(email);
}

function isPassword(password) {
  return /^[A-Za-z]\w{7,14}$/.test(password);
}
@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');
@import url('https://fonts.googleapis.com/css?family=Open Sans:400,500&display=swap');
* {
  box-sizing: border-box;
}

.main {
  margin-top: 50px;
}

body {
  background-color: plum;
  font-family: 'Open Sans', sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 25%;
  /* margin: 0; */
}

.container {
  background-color: white;
  border-radius: 5px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  width: 420px;
  max-width: 100%;
}

.header {
  border-bottom: 1px solid white;
  background-color: whitesmoke;
  padding: 20px 40px;
}

.header h2 {
  margin: 0;
}

.form {
  padding: 30px 40px;
}

.form-control {
  margin-bottom: 10px;
  padding-bottom: 20px;
  position: relative;
}

.form-control label {
  display: inline-block;
  margin-bottom: 5px;
}

.form-control input {
  border: 2px solid whitesmoke;
  border-radius: 4px;
  display: block;
  font-family: inherit;
  font-size: 14px;
  padding: 10px;
  width: 100%;
}

.form-control.success input {
  border-color: #2ecc71;
}

.form-control.error input {
  border-color: #e74c3c;
}

.form-control i {
  position: absolute;
  top: 40px;
  right: 10px;
  visibility: hidden;
}

.form-control.success i.fa-check-circle {
  color: #2ecc71;
  visibility: visible;
}

.form-control.error i.fa-exclamation-circle {
  color: #e74c3c;
  visibility: visible;
}

.form-control small {
  color: #e74c3c;
  visibility: hidden;
  position: absolute;
  bottom: 0;
  left: 0;
}

.form-control.error small {
  visibility: visible;
}

.form button {
  background-color: purple;
  border: 2px solid purple;
  border-radius: 4px;
  color: white;
  display: block;
  font-family: inherit;
  font-size: 16px;
  padding: 10px;
  width: 100%;
}

.footer p {
  font-size: 14px;
}

.main a {
  color: black;
}

.signUp,
.signIn {
  /*     position: absolute; */
}
<div >
  <div >
    <div >
      <div >
        <h2>Login</h2>
      </div>
      <form  id="form">
        <div >
          <label for="email">Email</label>
          <input type="email" placeholder="" id="email" />
        </div>
        <div >
          <label for="password">Password</label>
          <input type="password" id="password" />
        </div>
        <button>Submit</button>
      </form>
    </div>
    <div >
      <p>Not have an account? <a href="">Sign Up Here</a></p>
    </div>
  </div>


  <div id="signUpID" >
    <div >
      <div >
        <h2>Create Account</h2>
      </div>
      <form id="form" >
        <div >
          <label for="username">Username</label>
          <input type="text" placeholder="Yeroma" id="username" />
          <i ></i>
          <i ></i>
          <small>Error message</small>
        </div>
        <div >
          <label for="username">Email</label>
          <input type="email" placeholder="[email protected]" id="email" />
          <i ></i>
          <i ></i>
          <small>Error message</small>
        </div>
        <div >
          <label for="username">Password</label>
          <input type="password" placeholder="password" id="password" />
          <i ></i>
          <i ></i>
          <small>Error message</small>
        </div>
        <div >
          <label for="username">Password verification</label>
          <input type="password" placeholder="password" id="password2" />
          <i ></i>
          <i ></i>
          <small>Error message</small>
        </div>
        <button>Submit</button>
        <div >
          <br>
          <p>By clicking the Sign Up button, you agree to our </p>
          <p><a href="">Terms and Condition</a> and <a href="">Policy Privacy</a></p>
        </div>
      </form>
    </div>
    <div >
      <p>Already have an account? <a href="" id="dd">Login here</a></p>
    </div>
  </div>
</div>

CodePudding user response:

try with this code.

use the javascript function to show hide signIn and signUp Form

const signIn = document.getElementsByClassName('signIn')[0];
const signUp = document.getElementsByClassName('signUp')[0];
signUp.style.display = 'none';

function showSignUpForm() {
  signUp.style.display = 'block';
  signIn.style.display = 'none';

}

function showSignInForm() {
  signIn.style.display = 'block';
  signUp.style.display = 'none';
}

const form = document.getElementById('form');
const username = document.getElementById('username');
const email = document.getElementById('email');
const password = document.getElementById('password');
const password2 = document.getElementById('password2');

form.addEventListener('submit', (e) => {
  e.preventDefault();
  checkInputs();
});

function checkInputs() {
  const usernameValue = username.value.trim();
  const emailValue = email.value.trim();
  const passwordValue = password.value.trim();
  const password2Value = password2.value.trim();

  if (usernameValue === '') {
    setErrorFor(username, 'Username cannot be blank');
  } else {
    setSuccessFor(username);
  }

  if (emailValue === '') {
    setErrorFor(email, 'Email cannot be blank');
  } else if (!isEmail(emailValue)) {
    setErrorFor(email, 'Email is not valid');
  } else {
    setSuccessFor(email);
  }

  if (passwordValue === '') {
    setErrorFor(password, 'Password cannot be blank');
  } else if (!isPassword(passwordValue)) {
    setErrorFor(password, 'Password is not valid')
  } else {
    setSuccessFor(password);
  }

  if (password2Value === '') {
    setErrorFor(password2, 'Password2 cannot be blank');
  } else if (!isPassword(password2Value)) {
    setErrorFor(password2, 'Password is not valid');
  } else if (passwordValue !== password2Value) {
    setErrorFor(password2, 'Passwords does not match');
  } else {
    setSuccessFor(password2);
  }


}

function setErrorFor(input, message) {
  const formControl = input.parentElement;
  const small = formControl.querySelector('small');

  small.innerText = message;

  formControl.className = 'form-control error';
}

function setSuccessFor(input) {
  const formControl = input.parentElement;
  formControl.className = 'form-control success';
}

function isEmail(email) {
  return /^[a-zA-Z0-9.!#$%&'* /=?^_`{|}~-] @[a-zA-Z0-9-] (?:\.[a-zA-Z0-9-] )*$/.test(email);
}

function isPassword(password) {
  return /^[A-Za-z]\w{7,14}$/.test(password);
}

const signIn = document.getElementsByClassName('signIn')[0];
const signUp = document.getElementsByClassName('signUp')[0];
signUp.style.display = 'none';

function showSignUpForm() {
  signUp.style.display = 'block';
  signIn.style.display = 'none';

}

function showSignInForm() {
  signIn.style.display = 'block';
  signUp.style.display = 'none';
}
@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');
@import url('https://fonts.googleapis.com/css?family=Open Sans:400,500&display=swap');
* {
  box-sizing: border-box;
}

.main {
  margin-top: 50px;
}

body {
  background-color: plum;
  font-family: 'Open Sans', sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 25%;
  /* margin: 0; */
}

.container {
  background-color: white;
  border-radius: 5px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  width: 420px;
  max-width: 100%;
}

.header {
  border-bottom: 1px solid white;
  background-color: whitesmoke;
  padding: 20px 40px;
}

.header h2 {
  margin: 0;
}

.form {
  padding: 30px 40px;
}

.form-control {
  margin-bottom: 10px;
  padding-bottom: 20px;
  position: relative;
}

.form-control label {
  display: inline-block;
  margin-bottom: 5px;
}

.form-control input {
  border: 2px solid whitesmoke;
  border-radius: 4px;
  display: block;
  font-family: inherit;
  font-size: 14px;
  padding: 10px;
  width: 100%;
}

.form-control.success input {
  border-color: #2ecc71;
}

.form-control.error input {
  border-color: #e74c3c;
}

.form-control i {
  position: absolute;
  top: 40px;
  right: 10px;
  visibility: hidden;
}

.form-control.success i.fa-check-circle {
  color: #2ecc71;
  visibility: visible;
}

.form-control.error i.fa-exclamation-circle {
  color: #e74c3c;
  visibility: visible;
}

.form-control small {
  color: #e74c3c;
  visibility: hidden;
  position: absolute;
  bottom: 0;
  left: 0;
}

.form-control.error small {
  visibility: visible;
}

.form button {
  background-color: purple;
  border: 2px solid purple;
  border-radius: 4px;
  color: white;
  display: block;
  font-family: inherit;
  font-size: 16px;
  padding: 10px;
  width: 100%;
}

.footer p {
  font-size: 14px;
}

.main a {
  color: black;
}

.signUp,
.signIn {
  /*     position: absolute; */
}
<div >
  <div >
    <div >
      <div >
        <h2>Login</h2>
      </div>
      <form  id="form">
        <div >
          <label for="email">Email</label>
          <input type="email" placeholder="" id="email" />
        </div>
        <div >
          <label for="password">Password</label>
          <input type="password" id="password" />
        </div>
        <button>Submit</button>
      </form>
    </div>
    <div >
      <p>Not have an account? <a href="javascript:void(0);" onclick="showSignUpForm()">Sign Up Here</a></p>
    </div>
  </div>


  <div id="signUpID" >
    <div >
      <div >
        <h2>Create Account</h2>
      </div>
      <form id="form" >
        <div >
          <label for="username">Username</label>
          <input type="text" placeholder="Yeroma" id="username" />
          <i ></i>
          <i ></i>
          <small>Error message</small>
        </div>
        <div >
          <label for="username">Email</label>
          <input type="email" placeholder="[email protected]" id="email" />
          <i ></i>
          <i ></i>
          <small>Error message</small>
        </div>
        <div >
          <label for="username">Password</label>
          <input type="password" placeholder="password" id="password" />
          <i ></i>
          <i ></i>
          <small>Error message</small>
        </div>
        <div >
          <label for="username">Password verification</label>
          <input type="password" placeholder="password" id="password2" />
          <i ></i>
          <i ></i>
          <small>Error message</small>
        </div>
        <button>Submit</button>
        <div >
          <br>
          <p>By clicking the Sign Up button, you agree to our </p>
          <p><a href="">Terms and Condition</a> and <a href="">Policy Privacy</a></p>
        </div>
      </form>
    </div>
    <div >
      <p>Already have an account? <a href="javascript:void(0);" onclick="showSignInForm()" id="dd">Login here</a></p>
    </div>
  </div>
</div>

  • Related