Sprawdzanie poprawności wiadomości e-mail wewnątrz EditText [duplikat]

To pytanie ma już odpowiedź tutaj:

Chcę zweryfikować e-mail wprowadzony wewnątrz EditText i to kod, który już mam:

Final edittext textMessage = (EditText)findViewById (R. id.textMessage);

Final TextView text = (TextView) findViewById (R. id. text);

    textMessage.addTextChangedListener(new TextWatcher() { 
        public void afterTextChanged(Editable s) { 
            if (textMessage.getText().toString().matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+") && s.length() > 0)
            {
                text.setText("valid email");
            }
            else
            {
                text.setText("invalid email");
            }
        } 
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 
        public void onTextChanged(CharSequence s, int start, int before, int count) {} 
    }); 

Problem polega na tym, że gdy wprowadzam 3 znaki po"@", pojawia się komunikat "valid email", kiedy musi pojawić się, gdy wprowadzam kompletny e-mail.

Jakieś sugestie?

Dziękuję wszystkim!

Author: Alan Moore, 2011-10-02

9 answers

Po prostu zmień swoje Wyrażenie regularne w następujący sposób:

"[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+"

Ponieważ . (kropka) oznacza dopasowanie dowolnego pojedynczego znaku.Dodaj podwójny ukośnik przed kropką, aby utworzyć prawdziwą kropkę.

 23
Author: Yuanhai Shi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-02 11:16:53

Napisałem bibliotekę rozszerzającą EditText, która natywnie obsługuje niektóre metody walidacji i jest bardzo elastyczna.

Aktualne, jak piszę, natywnie obsługiwane (przez atrybuty xml) Metody walidacji to:

  1. regexp: dla niestandardowych regexp
  2. numeric : tylko dla pola numerycznego
  3. alpha : dla pola tylko Alfa
  4. / Align = "left" /
  5. email : sprawdza, czy pole jest poprawnym adresem e-mail
  6. creditCard : sprawdza, czy pole zawiera ważną kartę kredytową za pomocą algorytmu Luhna
  7. phone : sprawdza, czy pole zawiera prawidłowy numer telefonu
  8. domainName : sprawdza, czy pole zawiera poprawną nazwę domeny (zawsze przechodzi test na poziomie API
  9. ipAddress : sprawdza, czy pole zawiera poprawny adres ip webUrl: sprawdza, czy pole zawiera poprawny adres url (zawsze przechodzi test na poziomie API
  10. nocheck : niczego nie sprawdza. (Domyślnie)
Możesz to sprawdzić tutaj: https://github.com/vekexasia/android-form-edittext

Mam nadzieję, że ci się spodoba:)

Na podlinkowanej stronie znajdziesz również przykład walidacji poczty e-mail. Skopiuję relatywny fragment tutaj:

<com.andreabaccega.widget.FormEditText
       style="@android:style/Widget.EditText"
       whatever:test="email"
       android:id="@+id/et_email"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="@string/hint_email"
       android:inputType="textEmailAddress"
       />  

Istnieje również aplikacja testowa prezentująca możliwości biblioteki.

To jest zrzut ekranu z aplikacja sprawdzająca poprawność pola e-mail.

Walidacja wiadomości e-mail wykonana w bibliotece xml+

 15
Author: Andrea Baccega,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-25 23:39:56
public boolean validateEmail(String email) {

Pattern pattern;
Matcher matcher;
String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(email);
return matcher.matches();

}
 5
Author: kyogs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-28 06:51:14

Kilka dobrych opcji tutaj, w tym android.util.Wzory.EMAIL_ADDRESS dla API 8+.

Https://stackoverflow.com/a/7882950/1011746

 3
Author: mindriot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:48:18

Jeśli używasz API 8 lub nowszego, możesz użyć łatwo dostępnej klasy Patterns do walidacji wiadomości e-mail. Przykładowy kod:

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

Przypadkiem, jeśli obsługujesz poziom API niższy niż 8, możesz po prostu skopiować plik Patterns.java do swojego projektu i odwołać się do niego. Kod źródłowy Patterns.java można pobrać z tego linku

 3
Author: Mahendra Liya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-06 10:39:31

Nie rób tego kodem. Możesz użyć atrybutu Inputtype EditText.

    <EditText 
        android:id="@+id/edit_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"/>
 1
Author: Timuçin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-02 10:53:04

Spróbuj tego wzoru.....

EditText.addTextChangedListener (new TextWatcher () {

        @Override
        public void afterTextChanged(Editable s) {
            pattern = Pattern.compile(".+@.+\\.[a-z]+");
            matcher = pattern.matcher(editText.getText().toString());

            if(matcher.matches()) {
                Log.i("Test","--------Valid Email--------");
            }else {

                Log.i("Test","--------Invalid Email------");
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        } 
    }); 
 1
Author: Kri,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-02 11:43:54
private boolean validateEmailAddress(CharSequence emailAddress)
{

  if( Build.VERSION.SDK_INT >= 8 )
  {
    return android.util.Patterns.EMAIL_ADDRESS.matcher(emailAddress).matches();
  }

  Pattern pattern;
  Matcher matcher;
  String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
  pattern = Pattern.compile(EMAIL_PATTERN);
  matcher = pattern.matcher(emailAddress);

  return matcher.matches();
}
 0
Author: Sonia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-25 20:13:28

/ / zweryfikuj format adresu e-mail. [email protected]

public boolean emailValidator(String email) 
{
    Pattern pattern;
    Matcher matcher;
    final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    pattern = Pattern.compile(EMAIL_PATTERN);
    matcher = pattern.matcher(email);
    return matcher.matches();
}
 0
Author: Virag Brahme,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-05 13:58:37