0

Here is my code. Now I want to check this in if condition:

public static boolean isEmailValid(String email) { boolean isValid = false;
String expression = "^(([\\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})$";
CharSequence inputStr = email;

Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (!matcher.matches()) {
  isValid = true;
}
return isValid;
dda
  • 6,030
  • 2
  • 25
  • 34

4 Answers4

1

1st and your way:

try: your String expression Should be

 String expression = "^(([\\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})$";

2nd way

you can use android.util.Patterns.EMAIL_ADDRESS.matcher(inputStr).matches()

so your Method Should be

public static boolean isEmailValid(String email) {    
CharSequence inputStr = email;    
return android.util.Patterns.EMAIL_ADDRESS.matcher(inputStr).matches();
}
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
  • +1 for mentioning `android.util.Patterns.EMAIL_ADDRESS`, but note that expression in your 1st way is not correct - e.g. valid email: email@1.2.3.4 wouldn't be matched – lopisan Jul 17 '13 at 09:59
  • @lopisan i use 2nd way ! thanks for pointing out, ohh its not working for email@1.2.3.4 ! so, i have updated the answer as per your expression ! – Tarsem Singh Jul 17 '13 at 10:05
  • @Tarsem i m using this condition for check it further else if (isEmailValid(email)) { Toast.makeText(getApplicationContext(), "Email ID is not valid !.", Toast.LENGTH_LONG).show(); } – Gaurav Mehta Jul 17 '13 at 10:38
  • @GauravMehta ok good ! – Tarsem Singh Jul 17 '13 at 10:39
  • @taesem its not working it always goes through if i enter abc then also it passes the condition what i do now ?? :( – Gaurav Mehta Jul 17 '13 at 11:43
  • @GauravMehta it will return true if email is valid other wise it will return false so make sure you are using this code properly ! i am talking about 2nd and best way ! – Tarsem Singh Jul 17 '13 at 12:03
  • @terseem Sir can you give me the condition for your 2nd way :( i mean can i use this else if( isEmailValid("something@example.com") ) { Toast.makeText(getApplicationContext(),"Email ID is not valid !.", Toast.LENGTH_LONG).show(); // Email is valid } or something else :( – Gaurav Mehta Jul 17 '13 at 12:33
  • @GauravMehta dear you are doing wrong if you want to check that Email id is not Valid than it should be if(!isEmailValid("something@example.com") ) { Toast.makeText(getApplicationContext(),"Email ID is not valid !.", Toast.LENGTH_LONG).show();} else {//Email id is valid } – Tarsem Singh Jul 17 '13 at 12:42
  • @tarseem i m passing an intent from a button of activity1.java to activity2.java a button of activity3.java to activity2.java a button of activity4.java to activity2.java now i have a back button in activity2.java how i can guess that that the back button goes to activitya or activity3 or activity4 if you can post any code then please send Thanks in advance – Gaurav Mehta Jul 18 '13 at 04:38
  • Sorry @GauravMehta i am unable to get this properly ! you can ask new question with relevant code that you tried ! – Tarsem Singh Jul 18 '13 at 04:41
1

I would define your function like this:

public static boolean isEmailValid(String email) {
  String expression = "^(([\\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})$";

  Matcher matcher = Pattern.compile(expression, Pattern.CASE_INSENSITIVE).matcher(email);
  return matcher.matches();
}

And then test valid email like this:

if( isEmailValid("something@example.com") ) {
  // Email is valid
} else {
  // Email is not valid
}

PS: I didn't check the validity of your regexp, just arranged the rest of the code

lopisan
  • 7,720
  • 3
  • 37
  • 45
1

why can not use this is the best way

boolean isEmailValid(CharSequence email) {
   return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
1

Another option is the built in Patterns starting with API Level 8:

public final static boolean isValidEmail(CharSequence target) {
    if (target == null) {
        return false;
    } else {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
}