0

I am trying to do email validation after user type finish their email. However, it is not indicating that the email is valid or not. I place the javascript at the top to let it run while rendering but it doesn't work either. The code below is the screen for registration using javascript. This is rendered using nodejs. Is it due to the position of code or am I missing something?

RegisterScreen.js

const RegisterScreen = {
  render: () =>   
  `
      <script type="text/javascript"> 
      const email = document.getElementById("email")
      
      email.addEventListener('input',()=>{
        const emailBox = document.querySelector('.emailBox')
        const emailText = document.querySelector('.emailText')
        const emailPattern = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,63}$/
  
        if(email.value.match(emailPattern)){
          emailBox.classList.add('valid')
          emailBox.classList.remove('invalid')
          emailText.innerHTML = "Your Email Address in Valid"
        }else{
          emailBox.classList.add('invalid')
          emailBox.classList.remove('valid')
          emailText.innerHTML = "Must be a valid email address."
        }
      })
      </script>   
      <div class="form">
        <form id="register-form" action="#">
          <ul class="form-container">
            <li>
              <h2>Create Account</h2>
            </li> 
            <li>
              <label for="name">Name</label>
              <input type="name"
                name="name"     
                id="name"
                required />
            </li>
            <li>
              <label for="email" class="emailBox">Email</label>
              <input type="email"
                name="email"
                id="email"  
                required          
                />
              <span class="emailText"></span>
            </li>
            <li>
              <label for="password">Password</label>
              <input type="password"
                id="password"
                name="password"
                required        
                />
            </li>
            <li>
              <label for="re-password">Re-Enter Password</label>
              <input type="password"
                id="re-password"
                name="re-password"
                required            
                />
            </li>
            <li>
              <button type="submit" class="primary">
                Register
              </button>
            </li>
            <li>
              <div>Already have an account? <a href="/#/signin"> Sign-In </a>
              </div>
            </li>
          </ul>
        </form>
      </div>`,
}
export default RegisterScreen
2
  • Change to email.addEventListener('blur',()=>{ that way it will check the email address when the input loses focus. Commented May 9, 2021 at 4:35
  • it still didnt display anything though. it seems that even if i add console log to the javascript, no message appear too Commented May 9, 2021 at 4:44

1 Answer 1

2

Your javascript is executing on HTML elements that don't yet exist. Load the javascript up after the page loads:

<script type="text/javascript"> 

window.onload = function() {
      const email = document.getElementById("email")
      
      email.addEventListener('blur',()=>{
        const emailBox = document.querySelector('.emailBox')
        const emailText = document.querySelector('.emailText')
        const emailPattern = /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,63}$/
  
        if(email.value.match(emailPattern)){
          emailBox.classList.add('valid')
          emailBox.classList.remove('invalid')
          emailText.innerHTML = "Your Email Address in Valid"
        }else{
          emailBox.classList.add('invalid')
          emailBox.classList.remove('valid')
          emailText.innerHTML = "Must be a valid email address."
        }
      })
}
      </script>   
Sign up to request clarification or add additional context in comments.

1 Comment

@TitanCode - did this work for you? If it solved your issue, please accept it as the answer, thx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.