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>
shaedrich
  • 5,457
  • 3
  • 26
  • 42
diana
  • 23
  • 1
  • 1
  • 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? – ikhvjs Jun 09 '21 at 11:50
  • Does this answer your question? https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript – onlit Jun 09 '21 at 11:50
  • 1
    Why is the title about email validation but question itself about the password? – t.niese Jun 09 '21 at 11:51
  • See this [here](https://stackoverflow.com/questions/19605150/regex-for-password-must-contain-at-least-eight-characters-at-least-one-number-a) – Ahmed Tag Amer Jun 09 '21 at 11:55
  • You can find tools online to help testing your regular expressions, https://regexfiddler.com/ or https://www.freeformatter.com/regex-tester.html for example – Peter Krebs Jun 09 '21 at 11:55

1 Answers1

1

Just change regex to this.

const form = {
  password: {
    value: 'buffaloBill3#@$',
  },
  email: {
    value: 'hannibal@lecter.com'
  }
};


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);
Areg
  • 1,414
  • 1
  • 19
  • 39