如何检查edittext的文本是否是电子邮件地址?

如何检查edittext的文本是电子邮件地址或不使用javascript和正则expression式? 在这里,我使用inputtype="textEmailAddress"这是工作,但没有错误消息显示。

 /** * method is used for checking valid email id format. * * @param email * @return boolean true for valid false for invalid */ public static boolean isEmailValid(String email) { String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[AZ]{2,4}$"; Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(email); return matcher.matches(); } 

在这个函数中传递你的编辑文本string。

对于正确的电子邮件validation您需要服务器端身份验


请注意 ,Android中现在有一个内置的方法,请参阅下面的答案。

在Android 2.2+上使用这个:

 boolean isEmailValid(CharSequence email) { return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches(); } 

我写了一个扩展EditText的库,它支持本地一些validation方法,实际上非常灵活。

目前,正如我写的, 本地支持(通过xml属性 )validation方法是:

  1. 正则expression式 :用于自定义正则expression式
  2. 数字 :仅用于数字字段
  3. alpha :仅用于alpha字段
  4. alphaNumeric :猜猜看是什么?
  5. 电子邮件 :检查该字段是否是有效的电子邮件
  6. creditcard :使用Luhnalgorithm检查该字段是否包含有效的信用卡
  7. 电话 :检查该字段是否包含有效的电话号码
  8. domainName :检查该字段是否包含有效的域名(始终通过API Level <8的testing)
  9. ipAddress :检查该字段是否包含有效的ip地址webUrl:检查该字段是否包含有效的url(始终通过API Level <8的testing)
  10. nocheck :它不检查任何东西。 (默认)

你可以在这里查看: https : //github.com/vekexasia/android-form-edittext

希望你喜欢它 :)

在我链接的页面中,您还可以find电子邮件validation的示例。 我将在这里复制相关代码片段:

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

还有一个testing应用程序展示图书馆的可能性。

这是validation电子邮件字段的应用程序的屏幕截图。

电子邮件验证完成通过XML +库

请按照以下步骤

步骤1 :

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <EditText android:id="@+id/editText_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_below="@+id/textView_email" android:layout_marginTop="40dp" android:hint="Email Adderess" android:inputType="textEmailAddress" /> <TextView android:id="@+id/textView_email" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:text="Email Validation Example" /> </RelativeLayout> 

第2步:

 import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; 

第3步:

 public class MainActivity extends Activity { private EditText email; private String valid_email; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initilizeUI(); } /** * This method is used to initialize UI Components */ private void initilizeUI() { // TODO Auto-generated method stub email = (EditText) findViewById(R.id.editText_email); email.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub // TODO Auto-generated method stub Is_Valid_Email(email); // pass your EditText Obj here. } public void Is_Valid_Email(EditText edt) { if (edt.getText().toString() == null) { edt.setError("Invalid Email Address"); valid_email = null; } else if (isEmailValid(edt.getText().toString()) == false) { edt.setError("Invalid Email Address"); valid_email = null; } else { valid_email = edt.getText().toString(); } } boolean isEmailValid(CharSequence email) { return android.util.Patterns.EMAIL_ADDRESS.matcher(email) .matches(); } // end of TextWatcher (email) }); } } 

这里的电子邮件是你的电子邮件ID。

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

下面的代码应该对你有用。

 String email; check.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { checkEmail(email); if (checkMail) { System.out.println("Valid mail Id"); } } }); } } public static boolean checkEmail(String email) { Pattern EMAIL_ADDRESS_PATTERN = Pattern .compile("[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}" + ")+"); return EMAIL_ADDRESS_PATTERN.matcher(email).matches(); } 

Apache Commons Validator可以像其他答案中提到的那样使用。

步骤:1)从这里下载jar文件

步骤:2)将其添加到您的项目库

import:

 import org.apache.commons.validator.routines.EmailValidator; 

代码:

 String email = "myName@example.com"; boolean valid = EmailValidator.getInstance().isValid(email); 

并允许本地地址::

 boolean allowLocal = true; boolean valid = EmailValidator.getInstance(allowLocal).isValid(email); 

一个简单的方法

  private boolean isValidEmail(String email) { String emailRegex ="^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; if(email.matches(emailRegex)) { return true; } return false; } 

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})$"; // "^[\\w\\.-]+@([\\w\\-]+\\.)+[AZ]{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; } 
  I Hope this code is beneficial for you public class Register extends Activity { EditText FirstName, PhoneNo, EmailId,weight; Button Register; private static final Pattern EMAIL_PATTERN = Pattern .compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"); private static final Pattern USERFIRSTNAME_PATTERN = Pattern .compile("[a-zA-Z0-9]{1,250}"); private static final Pattern PHONE_PATTERN = Pattern .compile("[a-zA-Z0-9]{1,250}"); @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.register); Register=(Button) findViewById(R.id.register); FirstName=(EditText)findViewById(R.id.person_firstname); PhoneNo =(EditText)findViewById(R.id.phone_no); EmailId=(EditText)findViewById(R.id.email_id); weight=(EditText) findViewById(R.id.weight); Register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sFirstName= FirstName.getText().toString(); sPhoneNo= PhoneNo.getText().toString(); sEmailId= EmailId.getText().toString(); sweight= weight.getText().toString(); if(sFirstName.equals("")||sPhoneNo.equals("")||sEmailId.equals("")||sweight.equals("")) { if ((!CheckUsername(sFirstName))) { Toast.makeText(Register.this, "FirstName can not be null",Toast.LENGTH_LONG).show(); } else if ((!Checkphoneno(sPhoneNo))) { Toast.makeText(Register.this, "ENTER VALID mobile no ",Toast.LENGTH_LONG).show(); } else if ((!CheckEmail(sEmailId))) { Toast.makeText(Register.this, "ENTER VALID EMAIL ID",Toast.LENGTH_LONG).show(); } else if ((!Checkweight(sweight))) { Toast.makeText(Register.this, "ENTER Weight in kg",Toast.LENGTH_LONG).show(); } } } private boolean CheckEmail(String sEmailId) { return EMAIL_PATTERN.matcher(sEmailId).matches(); } private boolean CheckUsername(String sFirstName) { return USERFIRSTNAME_PATTERN.matcher(sFirstName).matches(); } private boolean Checkphoneno(String sPhoneNo) { return PHONE_PATTERN.matcher(sPhoneNo).matches(); } private boolean Checkweight(String sweight) { return Weight_PATTERN.matcher(sweight).matches(); } }); 

电子邮件validation试试这个。

 public boolean checkemail(String email) { Pattern pattern = Pattern.compile(".+@.+\\.[az]+"); Matcher matcher = pattern.matcher(email); return matcher.matches(); } 

你可以通过正则expression式来检查它

  public boolean isValid(String strEmail) { pattern = Pattern.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"); matcher = pattern.matcher(strEmail); if (strEmail.isEmpty()) { return false; } else if (!matcher.matches()) { return false; } else { return true; } } 
 private boolean isValidEmailID(String email) { String 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(PATTERN); Matcher matcher = pattern.matcher(email); return matcher.matches(); } 

点击这里查看更多validation

正如答案中提到的那样,您可以使用Patterns类如下:

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

如果你甚至支持小于8的API级别,那么你可以简单地将Patterns.java文件复制到你的项目中并引用它。 你可以从这个链接获取Patterns.java的源代码