0

I am trying to make a sign-up form that will do client-side validation (check the correct layout of email and matching passwords) before sending any data to the server. I have been having trouble checking to see if the email is in the correct form. I can't get the if(email.indexOf(@))to work correctly. I think I misused the .indexOf()

This is my JavaScript:

function sign_check() {
var email = document.getElementById("sign_email").value;
var user = document.getElementById("sign_user").value;
var pass = document.getElementById("sign_pass").value;
var passcon = document.getElementById("sign_confirm").value;

if(pass !== passcon){
  document.getElementById("sign_alert").innerHTML="The passwords do not match"
}

//This part determines whether or not to send the data to the server
if(email.length >= 7){
  if(email.indexOf("@")){
    if(user.length >= 1){
      if(pass.length >= 1){
        if(passcon.length >= 1){
          if(pass === passcon){
            alert("All of the requirements have been met")
          }
        }
      }
    }
  }
}
}

And this is my html:

  <h1 id="pop_up" class="pop_up">Sign Up</h1>

  <form id="sign_up" class="sign_up">
  <label id="alert_s1" class="alert">  <br />  </label>
    <input id="sign_email" class="sign" type="text" placeholder="Email" name="sign_email" /><br />
      <label id="alert_s2" class="alert">  <br />  </label>
    <input id="sign_user" class="sign" type="text" placeholder="Username" name="sign_user" /><br />
      <label id="alert_s3" class="alert">  <br />  </label>
    <input id="sign_pass" class="sign" type="text" placeholder="Password" name="sign_pass" /><br />
      <label id="alert_s4" class="alert">  <br />  </label>
    <input id="sign_confirm" class="sign" type="text" placeholder="Confirm Password" name="sign_confirm" />
  </form>

  <p id="sign_alert" class="alert"></p>

  <button onclick="sign_check()">Submit</button>

  <a href="javascript:void(0)" class="pop_up" id="pop_up" onclick="document.getElementById('sign_box').style.display='none'; document.getElementById('log_box').style.display='block'">Already have an acount? Click here to log in.</a>
</div>
justephens
  • 3
  • 1
  • 1
  • 2

2 Answers2

3

First, your method to validate the email is not very accurate :) Besides that, you're using indexOf incorrectly. Use this instead.

if(email.indexOf("@") != -1 )
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • 1
    or > 0. Since `@gmail.com` isn't a valid e-mail either. – leigero Mar 09 '14 at 00:12
  • 1
    email validation doesn't really need to be accurate. It's much too complex to bother with, and even if you have a perfect email validation, you still don't know if the email is valid in the sense that it's actually owned by a user. Better to be too permissive than too restrictive, IMO. – cookie monster Mar 09 '14 at 00:13
  • 1
    @leigero: I've already clarified that the validation method is not accurate. There are infinite invalid combinations that would be ignored – Claudio Redi Mar 09 '14 at 00:13
  • @cookie monster: in any case you could have a better validation rule to cover ovbious mistakes. No need to get complex. – Claudio Redi Mar 09 '14 at 00:16
  • I agree. Just saying it would be worse to unintentionally prevent someone from submitting. – cookie monster Mar 09 '14 at 00:20
-1

You used double quotes "" instead of '' in the ('@'). That should make it work. And === actually works too. The quotation marks were all you needed to change. I hope this helps!

Anders
  • 8,307
  • 9
  • 56
  • 88
Eni
  • 1