0

this is my email address validation using JavaScript, I have only two conditions that work, the one with the special character is not working.
Only the third is tested on the email address, the first two are on the password. Please help me.

<script type = "text/javascript"> 
        function validateEmail(form) {
            var tr = /[A-Za-z0-9]{8,}/
            var tr2 = /\S+@\S+\.\S+/  
            var tr3 = /[A-Za-z0-9]+[a-zA-Z0-9.!$+/?_]/
            if (!(tr.test(form.password.value))){
                alert("The passowrd length must be more than 8 letters.")
                return false;
            }
            else if (!(tr3.test(form.password.value))){
                alert("Enter a special character.")
                return false;
            }
            else if (!(tr2.test(form.email.value))){
                alert("Enter a valid email.")
                return false;
            }
            else{
                alert("signed in successfully")
                window,open("HomePage.html")
                return true
            }
        }
    </script>
5
  • I have only two conditions that work, the one with the special character is not working. doesn't help people understand your question. What is your expected result? Commented Jun 9, 2021 at 11:50
  • Does this answer your question? stackoverflow.com/questions/46155/… Commented Jun 9, 2021 at 11:50
  • 1
    Why is the title about email validation but question itself about the password? Commented Jun 9, 2021 at 11:51
  • See this here Commented Jun 9, 2021 at 11:55
  • You can find tools online to help testing your regular expressions, regexfiddler.com or freeformatter.com/regex-tester.html for example Commented Jun 9, 2021 at 11:55

1 Answer 1

1

Just change regex to this.

const form = {
  password: {
    value: 'buffaloBill3#@$',
  },
  email: {
    value: '[email protected]'
  }
};


function validateEmail(form) {
  var tr = /[A-Za-z0-9]{8,}/
  var tr2 = /\S+@\S+\.\S+/
  var tr3 = /^(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{0,}$/;
    
  if (!(tr.test(form.password.value))) {
    alert("The passowrd length must be more than 8 letters.")
    return false;
  } else if (!(tr3.test(form.password.value))) {
    alert("Enter a special character.")
    return false;
  } else if (!(tr2.test(form.email.value))) {
    alert("Enter a valid email.")
    return false;
  } else {
    alert("signed in successfully")
    window, open("HomePage.html")
    return true
  }
}

validateEmail(form);

Sign up to request clarification or add additional context in comments.

Comments

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.