1

I know this question has been asked many times but my question is different from others. Currently, I am using below regex for validating email address but there is a case when I test for .com.com multiple times it accepts. I want to a regex that also test for multiple .com or multiple .in . is that possible? Or should I handle this programmatically like I split whole string with expression “.” And check for array length. If length is greater that 1 it means multiple . com/.in exists and should not validate? The regex I am using is below

 String regExpn =
    "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";
Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
  • slight problem with that idea. You have to remember some people format their addresses like: some.name@gmail.com and that would lead there to be three parts instead of two – Zoe Jul 18 '16 at 11:42
  • Possible duplicate of [How should I validate an e-mail address?](http://stackoverflow.com/questions/1819142/how-should-i-validate-an-e-mail-address) – Alexander Mironov Jul 18 '16 at 12:32

3 Answers3

0
public static final Pattern EMAIL_PATTERN = Pattern
        .compile("^(?:[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\""
                + "(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\"
                + "[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[a-z0-9])?\\.)"
                + "+[A-Za-z0-9](?:[A-Za-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.)"
                + "{3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[A-Za-z0-9-]*[A-Za-z0-9]:"
                + "(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\"
                + "[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])$");

I made it multiline to not have a too long line. I'm using this for email validation when registering users.

finki
  • 2,063
  • 1
  • 20
  • 23
0

Simply I suggest,

if(email.substring(email.lastIndexOf("@") + 1).chars().filter(ch -> ch == '.').count() >=2 ){
  //contains .in
}
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
  • Not resolved by above solution because android studio is not identifying chars() method even I am using java 8 :( – Abdul Waheed Jul 19 '16 at 07:08
  • I've tested this in simple java console with formal string, And working perfectly. There are lots of way to check the repeated occurrences of some specific character, So the logic behind this is **Check the (DOT)'s occurrence in the string after @ symbol and if It's greater than or equals than 2, then it contains .com.in else only .com.** Did you get me ? – Shree Krishna Jul 19 '16 at 07:11
  • You can use some core looping method or apache's stringutils class to check the occurrence of some character in a string. – Shree Krishna Jul 19 '16 at 07:15
  • yes i got you and I have implemented that already. I wanted to know is there any other way via regex or any built in method in android – Abdul Waheed Jul 19 '16 at 07:16
  • No there is not any built in method for this, Regex is an alternate, But you have to check regex multiple times and check with which regex the current email matched. So that I think regex is not a better approach for this. The solution I gave was better approach but you told `chars()` not recognized, so that In my view It's better using some alternate self created methods for checking the occurrence of DOT. – Shree Krishna Jul 19 '16 at 08:37
  • You can leave the question open for some time, Feel free to accept after being sure that no solution will come further, You're most welcome, – Shree Krishna Jul 19 '16 at 09:21
0

Here is how I solved my problem as mentioned in above question.

I used String split() function first with the expression "@" and got String on first index and then further split with expression "\." and got the length of array string if length is greater that 2 it means .com / .in contains multiple times below is the code:

  public static boolean isEmailValid(String email) {

    CharSequence inputStr = email;
    Pattern pattern = Pattern.compile(regExpn,Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);

    if(matcher.matches()) {
        String [] splitedEmail = email.split("@");
        String toCheckMultipleDots = splitedEmail[1];
        String [] toChecksplittedDots = toCheckMultipleDots.split("\\.");

        if(toChecksplittedDots.length > 2){
            return false;
        } else {
            return true;
        }
    }

    else
        return false;
}
Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58