Android上的电子邮件validationEditText

大家好我有一个编辑文本,我会写在我的Editttext电子邮件validation这是一个XML代码

<EditText android:id="@+id/mail" android:layout_width="match_parent" android:layout_height="48dp" android:layout_alignLeft="@+id/phone" android:layout_below="@+id/phone" android:layout_marginRight="33dp" android:layout_marginTop="10dp" android:background="@drawable/edit_background" android:ems="10" android:hint="E-Mail" android:inputType="textEmailAddress" android:paddingLeft="20dp" android:textColor="#7e7e7e" android:textColorHint="#7e7e7e" > </EditText> 

这是一个Java代码

 emailInput = mail.getText().toString().trim(); emailPattern = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; if (emailInput.matches(emailPattern)) { Toast.makeText(getActivity(), "valid email address", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getActivity(), "Invalid email address", Toast.LENGTH_SHORT).show(); mail.setBackgroundResource(R.drawable.edit_red_line); } 

我无法validation。吐司消息总是“无效的电子邮件地址”我做错了什么? 如果有人知道解决scheme,请帮助我

Solutions Collecting From Web of "Android上的电子邮件validationEditText"

为什么不使用:

 public final static boolean isValidEmail(CharSequence target) { return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches(); } 

如这里所build议的。

我张贴非常简单和容易的电子邮件validation答案,而不使用任何string模式。

1.设置button上的点击监听器….

  button_resetPassword.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CharSequence temp_emilID=username.getText().toString();//here username is the your edittext object... if(!isValidEmail(temp_emilID)) { username.requestFocus(); username.setError("Enter Correct Mail_ID ..!!"); or Toast.makeText(getApplicationContext(), "Enter Correct Mail_ID", Toast.LENGTH_SHORT).show(); } else { correctMail.. //Your action... } }); 

2.致电isValidEmail()ie。

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

我希望这对你有帮助

Android电子邮件validation最简单的方法

  String validemail= "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@" + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "(" + "\\." + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + ")+"; String emal=email.getText().toString(); Matcher matcherObj = Pattern.compile(validemail).matcher(emal); if (matcherObj.matches()) { Toast.makeText(getApplicationContext(), "enter all details", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(),"please enter valid email",Toast.LENGTH_SHORT).show(); } 

尝试下面的代码:

 public final static boolean isValidEmail(CharSequence target) { return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches(); } 

这工作正常。

android.util.Patterns.EMAIL_ADDRESS.matcher(目标).matches()

这里是一个完整的loginvalidation代码……

 public class LoginActivity extends AppCompatActivity { private static final String TAG = "LoginActivity"; private static final int REQUEST_SIGNUP = 0; @Bind(R.id.input_email) EditText _emailText; @Bind(R.id.input_password) EditText _passwordText; @Bind(R.id.btn_login) Button _loginButton; @Bind(R.id.link_signup) TextView _signupLink; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); ButterKnife.bind(this); _loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { login(); } }); _signupLink.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Start the Signup activity Intent intent = new Intent(getApplicationContext(), SignupActivity.class); startActivityForResult(intent, REQUEST_SIGNUP); finish(); overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out); } }); } public void login() { Log.d(TAG, "Login"); if (!validate()) { onLoginFailed(); return; } _loginButton.setEnabled(false); final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this, R.style.AppTheme_Dark_Dialog); progressDialog.setIndeterminate(true); progressDialog.setMessage("Authenticating..."); progressDialog.show(); String email = _emailText.getText().toString(); String password = _passwordText.getText().toString(); // TODO: Implement your own authentication logic here. new android.os.Handler().postDelayed( new Runnable() { public void run() { // On complete call either onLoginSuccess or onLoginFailed onLoginSuccess(); // onLoginFailed(); progressDialog.dismiss(); } }, 3000); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_SIGNUP) { if (resultCode == RESULT_OK) { // TODO: Implement successful signup logic here // By default we just finish the Activity and log them in automatically this.finish(); } } } @Override public void onBackPressed() { // Disable going back to the MainActivity moveTaskToBack(true); } public void onLoginSuccess() { _loginButton.setEnabled(true); finish(); } public void onLoginFailed() { Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show(); _loginButton.setEnabled(true); } public boolean validate() { boolean valid = true; String email = _emailText.getText().toString(); String password = _passwordText.getText().toString(); if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) { _emailText.setError("enter a valid email address"); valid = false; } else { _emailText.setError(null); } if (password.isEmpty() || password.length() < 4 || password.length() > 10) { _passwordText.setError("between 4 and 10 alphanumeric characters"); valid = false; } else { _passwordText.setError(null); } return valid; } 

}

私人无效isValidEmail(stringemail_id){

  if (email_id == null){ checkTextView.setVisibility(View.VISIBLE); checkTextView.setText(LocaleController.getString("EnterValidEmail", R.string.EnterValidEmail)); return; } if (android.util.Patterns.EMAIL_ADDRESS.matcher(email_id).matches()) { checkTextView.setVisibility(View.GONE); } else { checkTextView.setVisibility(View.VISIBLE); checkTextView.setText(LocaleController.getString("EnterValidEmail", R.string.EnterValidEmail)); } 

采取textView ex.checkTextView并validation电子邮件是否有效,然后textview消失,否则显示消息

使用此functionvalidation电子邮件ID:

  private boolean validateEmaillId(String emailId){ return Pattern.compile("^(([\\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(emailId).matches(); } 

我有一个电子邮件模式查询多个电子邮件IDvalidation和只有一个电子邮件ID.I解决它通过使用:

 public static final String patter_emails="^(\\s*,?\\s*[0-9a-za-z]([-.\\w]*[0-9a-za-z])*@([0-9a-za-z][-\\w]*[0-9a-za-z]\\.)+[a-za-z]{2,9})+\\s*$"; public static final String patter_email="^(\\s*[0-9a-za-z]([-.\\w]*[0-9a-za-z])*@([0-9a-za-z][-\\w]*[0-9a-za-z]\\.)+[a-za-z]{2,9})+\\s*$"; 

以上这些用于Java和Android两种模式。

使用以下方法检查:

模式testing:rubular.com

尝试下面的代码:

只要调用下面的方法,

 if(emailValidator(mail.getText().toString())){ Toast.makeText(getActivity(), "valid email address", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getActivity(), "invalid email address", Toast.LENGTH_SHORT).show(); } public static boolean emailValidator(final String mailAddress) { 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(mailAddress); return matcher.matches(); } 

分配一个stringvariables来存储这个EditText的值:

 emailInput = mail.getText().toString().trim(); 

在EditText中使用setError:

 if(!isValidEmail(emailInput)){ mail.setError("Invalid"); /*"Invalid Text" or something like getString(R.string.Invalid)*/ mail.requestFocus(); } 

创build一个方法来检查电子邮件:

 private boolean isValidEmail(String emailInput) { 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 = Pattern.compile(EMAIL_PATTERN); Matcher matcher = pattern.matcher(emailInput); return matcher.matches(); }