3 Replies Latest reply: May 30, 2007 10:56 PM by 807606 RSS

    Validating e-mail address format

    807606
      It's been asked plenty of times, but the only answer I found to work for me was the use of regular expressions. I was able to get this bit of code to work:
      if (!(t_email.getText ()).matches("\\w*\\.\\w*@\\w*\\.\\w*"))  //check for valid and existing e-mail
              {
                  JOptionPane.showMessageDialog (CreateAccount.this,"invalid." , "Error",
                          JOptionPane.ERROR_MESSAGE);
              }
      That code accepts e-mail addresses in the format of: eg. tristan.lee@domain.com Anything like tristan@domain.com is considered invalid for that expression.

      I figured that
      if (!(t_email.getText ()).matches("[a-z[._-][\\d]]*[@][a-z[.][\\d]]*[.][a-z[.][\\d]]*"))  //check for valid and existing e-mail
              {
                  JOptionPane.showMessageDialog (CreateAccount.this,"invalid." , "Error",
                          JOptionPane.ERROR_MESSAGE);
              }
      would work, but it says that tristan@domain..com is valid. You can't have 2 periods before the com/net/org, etc can you?