tags:

views:

91

answers:

5

Hi I have designed a chat application using servlets and jsp. I have designed a page for user login, where I accept used id and his mail-id.How can i ensure that user has typed proper mail-id. I need a code to validate email-id.I mean for example user can not type anything in the text box.

+1  A: 

You'll want to make sure the provided value passes a regular expression for a valid email address.

Read more: http://www.regular-expressions.info/email.html

Jason Hall
+2  A: 

You need to validate the email. The easiest way is to use a regular expression: There is information here. This of course, would be on the client side, when you validate your form.

If you are using hibernate bean validation, there is an @Email annotation that you can use as well.

Vivin Paliath
+2  A: 

The Apache Commons EmailValidator does this if you are doing server-side validation. If you are concerned about client-side validation then you'll need to create a regular expression and match against that using JavaScript.

EmailValidator validator = EmailValidator.getInstance();
if (validator.isValid(yourEmail)) {
    // valid e-mail
} else {
    // invalid e-mail
}
Taylor Leese
+1  A: 

Here's a simple implementation of the general concept other posters have described, using JavaScript:

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

(from http://www.codetoad.com/javascript/is_valid_email.asp)

Using regex to validate an email address in Java:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailValidator {

      private Pattern pattern;
      private Matcher matcher;

      private static final String EMAIL_PATTERN = 
                   "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@
                   [A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

      public EmailValidator(){
          pattern = Pattern.compile(EMAIL_PATTERN);
      }

      /**
       * Validate hex with regular expression
       * @param hex hex for validation
       * @return true valid hex, false invalid hex
       */
      public boolean validate(final String hex) {

          matcher = pattern.matcher(hex);
          return matcher.matches();
      }
}

(from http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/)

Each link was either #1 or #2 on a Google search.

spork
+1  A: 

Most of the current regex email validators doesn't take into account that "unicode characters" like Chinese and Arabic in domain names are to be allowed in the near future. Everyone has to change the email regex used, because those characters are surely not to be covered by [a-z]/i nor \w in common regex solutions. They will all fail.

After all, the best way to validate the email address is still to actually send an email to the address in question to validate the address. The enduser can namely stil enter a syntactically valid emailaddress like [email protected] but which is obviously non-existent. If the email address is part of user authentication (register/login/etc), then you can perfectly combine it with the user activation system. I.e. send an email with a link with an unique activation key to the specified email address and only allow login when the user has activated the newly created account using the link in the email.

If the purpose of the regex is just to quickly inform the user in the UI that the specified email address doesn't look like in the right format, best is still to check if it matches basically the following regex:

^([^.@]+)(\.[^.@]+)*@([^.@]+\.)+([^.@]+)$

Simple as that. No need to care about the characters used in the name and domain. It's after all the client's responsibility to enter a legit email address, not the server's.

In Java you could use the above regex as:

public static boolean isValidEmail(String value) {
    return value.matches("^([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)$");
}
BalusC
+1 for unicode symbols.
St.Shadow