I am trying to validate email in java. Below is the code:
String mail = "[email protected]"; String regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; if(mail.matches(regex)){ System.out.println("valid email"); }; Now i want to restrict the length of Text1 and Text2 to 10 characters and length of Text3 to 5 characters.
I tried with this regex but did not work - ^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]{2,}+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$
How do i achieve this? Thanks.
.+@.+or.+@.+\..+. Different providers allow different characters in email address. According to email address specification, you can actually put any character in there given that you escape it. Gmail for example allows the plus sign before @, for which I don't see handling in your regex. Also, gmail actually allows a dot as the first character which is invalid according to email address specification.