开发者

Validate an email inside an EditText [duplicate]

开发者 https://www.devze.com 2023-04-09 18:25 出处:网络
This question already has answers here: How to check edittext's text is email address or not? (20 answers)
This question already has answers here: How to check edittext's text is email address or not? (20 answers) Closed 8 years ago.

I want to validate an email introduced inside an EditText and this the code that I already have:

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]+") && 开发者_如何学Gos.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) {} 
    }); 

The problem is that when I introduce 3 characters after the "@", it appears the message "valid email", when it must appear when I introduce the complete email.

Any suggerence?

Thank you all!


Just change your regular expression as follows:

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

Because . (dot) means match any single-char.ADD a double backslash before your dot to stand for a real dot.


I wrote a library that extends EditText which supports natively some validation methods and is actually very flexible.

Current, as I write, natively supported (through xml attributes) validation methods are:

  1. regexp: for custom regexp
  2. numeric: for an only numeric field
  3. alpha: for an alpha only field
  4. alphaNumeric: guess what?
  5. email: checks that the field is a valid email
  6. creditCard: checks that the field contains a valid credit card using Luhn Algorithm
  7. phone: checks that the field contains a valid phone number
  8. domainName: checks that field contains a valid domain name ( always passes the test in API Level < 8 )
  9. ipAddress: checks that the field contains a valid ip address webUrl: checks that the field contains a valid url ( always passes the test in API Level < 8 )
  10. nocheck: It does not check anything. (Default)

You can check it out here: https://github.com/vekexasia/android-form-edittext

Hope you enjoy it :)

In the page I linked you'll be able to find also an example for email validation. I'll copy the relative snippet here:

<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"
       />  

There is also a test app showcasing the library possibilities.

This is a screenshot of the app validating the email field.

Validate an email inside an EditText [duplicate]


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();

}


If you are using API 8 or above, you can use the readily available Patterns class to validate email. Sample code:

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

By chance if you are even supporting API level less than 8, then you can simply copy the Patterns.java file into your project and reference it. You can get the source code for Patterns.java from this link


Several good options here including android.util.Patterns.EMAIL_ADDRESS for API 8+.

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


Don't do it in code. You can use inputType attribute of EditText.

    <EditText 
        android:id="@+id/edit_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"/>


Try this pattern.....

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) {

        } 
    }); 


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();
}


// validate your email address format. Ex-abci@gmail.com

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

精彩评论

暂无评论...
验证码 换一张
取 消