0

I'm working on GUI validation... Please see the problem below...

How to validate an email with a specific format? at least one digit before the @ and one digit after and at least two letters after the dot.

String EmailFormat = "m@m.co";
Pattern patternEmail = Pattern.compile("\\d{1,}@\\d{1,}.\\d{2,}");
            Matcher matcherName = patternEmail.matcher(StudentEmail);
Whizz
  • 5
  • 5
  • 1
    If you are looking for some patterns and a good read about email validation, have a look here http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – cheffe Jun 07 '14 at 14:39
  • `.` means any character. `\.` and `[.]` mean literal dot. `\\d` means digit, not letter. – Mike Samuel Jun 07 '14 at 14:43
  • ("[A-Z0-9._%+-]{1,10}+@[A-Z0-9.-]{1,10}+\\.[A-Z]{2,4}$"); Is this a right way of doing it? I need a specific letter constraints between the dot and the @ – Whizz Jun 07 '14 at 14:50
  • @Whizz, maybe don't use a regular expression. Use an email parsing library to get the mailbox name, and separate out the local part from the domain. Then write a predicate that checks the domain before the first dot. – Mike Samuel Jun 07 '14 at 15:15

3 Answers3

1

Don't write your own validator. Email has been around for decades and there are many standard libraries which work, address parts of the standard you may not know about, and are well tested by many other developers.

Apache Commons Email Validator is a good example. Even if you use a standard validator you need to be aware of the limitations or gotchas in validating an email address. Here are the javadocs for Commons EmailValidator which state, "This implementation is not guaranteed to catch all possible errors in an email address. For example, an address like nobody@noplace.somedog will pass validator, even though there is no TLD "somedog"" . So you can use a good email validator to determine if an address is valid, but you will have to do extra work to guarantee that the domain exists, accepts email, and accepts email fro that address.

If you require good addresses you will need a secondary mechanism. A confirmation email is a good mechanism. You send a link to the given address and the user must visit that link to verify that email can be sent to that address.

Freiheit
  • 8,408
  • 6
  • 59
  • 101
0
  1. Split email into two parts using @ as delimiter:

    String email = "some@email.com";
    String[] parts = email.split("@"); // parts = [ "some", "email.com" ]
    
  2. Validate each part separately, using multiple checks if necessary:

    // validate username
    String username = parts[0];
    
    if (username.matches("\\d")) {
        // ok
    }
    
    // validate domain
    String domain = parts[1];
    
    if (domain.matches("\\d") && domain.matches("\\.[a-z]{2,4}$")) {
        // ok
    }
    

Note that this is a very poor email validator and it shouldn't be used standalone.

Crozin
  • 43,890
  • 13
  • 88
  • 135
  • ("[A-Z0-9._%+-]{1,10}+@[A-Z0-9.-]{1,10}+\\.[A-Z]{2,4}$"); Is this a right way of doing it? I need a specific letter constraints between the dot and the @ – Whizz Jun 07 '14 at 14:51
  • I like your remark about not using this standalone.This regex will probably miss http://en.wikipedia.org/wiki/.museum which has been around since 2001. `.localhost` is also a valid TLD. Granted those are edge cases, but we live in interesting times with new TLDs. – Freiheit Jun 07 '14 at 14:56
0

This the regex pattern for emails

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

You can try it like this

    List email = Arrays.asList("xyzl@gmail.com", "@", "sxd");
    Predicate<String> validMail = (n) -> n.matches(pt);
    email.stream().filter(validMail).forEach((n) -> System.out.println(n));

This is the description you can change it according to your need.

    ^           #start of the line
  [_A-Za-z0-9-\\+]+ #  must start with string in the bracket [ ], must contains one or more (+)
  (         #   start of group #1
    \\.[_A-Za-z0-9-]+   #     follow by a dot "." and string in the bracket [ ], must contains one or more (+)
  )*            #   end of group #1, this group is optional (*)
    @           #     must contains a "@" symbol
     [A-Za-z0-9-]+      #       follow by string in the bracket [ ], must contains one or more (+)
      (         #         start of group #2 - first level TLD checking
       \\.[A-Za-z0-9]+  #           follow by a dot "." and string in the bracket [ ], must contains one or more (+)
      )*        #         end of group #2, this group is optional (*)
      (         #         start of group #3 - second level TLD checking
       \\.[A-Za-z]{2,}  #           follow by a dot "." and string in the bracket [ ], with minimum length of 2
      )         #         end of group #3
$           #end of the line
Raj
  • 942
  • 1
  • 7
  • 12