-1

i am making a password and email page and need to validate the email with a reg-ex. my problem is the reg-ex i use will only validate to after the @ sign after that point all emails are valid,i need a full proof reg-ex that validates and makes sure its either ".com" or the English alternative (spell check wont accept) at the end can anyone help?

My code for anyone needing it:

    <script type="text/javascript">

     function checkForm(form)
    {
    if(form.email.value == "") {
      alert("Error: Username cannot be blank!");
      form.email.focus();
      return false;
    }
    re = /^\w+$/;
    if(!re.test(form.email.value)) {
      alert("Error: Username must contain only letters, numbers and underscores!");
      form.email.focus();
      return false;
    }

    if(form.pwd1.value != "" && form.pwd1.value == form.pwd2.value) {
      if(form.pwd1.value.length < 6) {
        alert("Error: Password must contain at least six characters!");
        form.pwd1.focus();
        return false;
      }
      if(form.pwd1.value == form.email.value) {
        alert("Error: Password must be different from Username!");
        form.pwd1.focus();
        return false;
      }
      re = /[0-9]/;
      if(!re.test(form.pwd1.value)) {
        alert("Error: password must contain at least one number (0-9)!");
        form.pwd1.focus();
        return false;
      }
      re = /[a-z]/;
      if(!re.test(form.pwd1.value)) {
        alert("Error: password must contain at least one lowercase letter (a-z)!");
        form.pwd1.focus();
        return false;
      }
      re = /[A-Z]/;
      if(!re.test(form.pwd1.value)) {
        alert("Error: password must contain at least one uppercase letter (A-Z)!");
        form.pwd1.focus();
        return false;
      }
    } else {
      alert("Error: Please check that you've entered and confirmed your password!");
      form.pwd1.focus();
      return false;
    }

    alert("You entered a valid password: " + form.pwd1.value);
    return true;
  }

   </script>

   <form ... onsubmit="return checkForm(this);">
   <p>email: <input type="email" name="email"></p>
   <p>Password: <input type="password" name="pwd1"></p>
   <p>Confirm Password: <input type="password" name="pwd2"></p>
   <p><input type="submit"></p>
   </form>
Alan Moore
  • 68,531
  • 11
  • 88
  • 149
  • [This relevant answer](http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address) comes from the [Stack Overflow Regular Expressions FAQ](http://stackoverflow.com/a/22944075/2736496). It's listed under "Common Tasks" – aliteralmind Oct 15 '14 at 00:27

1 Answers1

1

The real regex to properly validate email addresses according to the spec is massive, and probably not what you want to actually do. Remember that .NET, .COM, .TRAVEL, .CA, and many many more are valid top-level domains and can have email sent to and from them.

Instead, consider doing very simple regex validation (check for an @ sign) and send a verification email to confirm that the email address is valid. Rely on the user to input their madness-inducing emails correctly.

See this relevant Programmers discussion.

Community
  • 1
  • 1
Cellivar
  • 530
  • 9
  • 27