如果不使用edittext
和正则表达式检查javascript
的文本是否为电子邮件地址?
在这里,我使用inputtype="textEmailAddress"
这是有效的,但没有显示错误消息。
答案 0 :(得分:376)
在Android 2.2+上使用此:
boolean isEmailValid(CharSequence email) {
return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
例如:
EditText emailid = (EditText) loginView.findViewById(R.id.login_email);
String getEmailId = emailid.getText().toString();
// Check if email id is valid or not
if (!isEmailValid(getEmailId)){
new CustomToast().Show_Toast(getActivity(), loginView,
"Your Email Id is Invalid.");
}
答案 1 :(得分:215)
/**
* 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\\-]+\\.)+[A-Z]{2,4}$";
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
在此功能中传递您的编辑文本字符串。
对于正确的电子邮件验证,您需要服务器端身份验证
注意 Android现在有一个内置方法,请参阅下面的答案。
答案 2 :(得分:12)
我编写了一个扩展EditText的库,它本身支持一些验证方法,实际上非常灵活。
目前,正如我所写,原生支持(通过 xml属性)验证方法是:
您可以在此处查看:https://github.com/vekexasia/android-form-edittext
希望你喜欢它:)
在我链接的页面中,您还可以找到电子邮件验证的示例。我将复制相关片段:
<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"
/>
还有一个测试应用程序展示了库的可能性。
这是验证电子邮件字段的应用的屏幕截图。
答案 3 :(得分:12)
请按照以下步骤
第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)
});
}
}
答案 4 :(得分:7)
正如其中一个答案所述,您可以使用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
文件复制到项目中并引用它即可。您可以从this link
Patterns.java
的源代码
答案 5 :(得分:2)
此处电子邮件是您的电子邮件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();
}
答案 6 :(得分:2)
以下代码对您有用。
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();
}
答案 7 :(得分:2)
Apache Commons Validator可以像其他答案中提到的那样使用。
步骤:1)从here
下载jar文件步骤:2)将其添加到项目库中
导入:
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);
答案 8 :(得分:2)
对于电子邮件地址验证,请尝试以下简单代码。
String email = inputEmail.getText().toString().trim();
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches())
{
inputEmail.setError("Enter Valid Email Address");
inputEmail.requestFocus();
}
答案 9 :(得分:1)
一个简单的方法
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;
}
答案 10 :(得分:1)
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\\-]+\\.)+[A-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;
}
答案 11 :(得分:1)
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();
}
});
答案 12 :(得分:0)
试试这个:
public boolean isValidEmail(String email) {
return (PatternsCompat.EMAIL_ADDRESS.matcher(email).matches());
}
谢谢!
答案 13 :(得分:0)
在Kotlin中,您可以通过简单的方法验证电子邮件地址,而无需编写大量代码,并使用诸如“ ^ [_ A-Za-z0-9-\ +] ...等正则表达式来烦恼自己。 “。
看如何简单:
fun validateEmail(emailForValidation: String): Boolean{
return Patterns.EMAIL_ADDRESS.matcher(emailForValidation).matches()
}
编写此方法进行电子邮件验证之后,您只需要输入要验证的电子邮件即可。 如果validateEmail()方法返回的是正确的电子邮件,则为false,则表明该电子邮件无效。
以下是如何使用此方法的示例:
val eMail: String = emailEditText.text.toString().trim()
if (!validateEmail(eMail)){ //IF NOT TRUE
Toast.makeText(context, "Please enter valid E-mail address", Toast.LENGTH_LONG).show()
return //RETURNS BACK TO IF STATEMENT
}
答案 14 :(得分:0)
使用android.util.Patterns和Kotlin非常简单。一行函数返回布尔值。
fun validateEmail(email: String) = Patterns.EMAIL_ADDRESS.matcher(email)
答案 15 :(得分:0)
onPress
答案 16 :(得分:0)
您可以通过正则表达式
进行检查 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;
}
}
答案 17 :(得分:0)
用于电子邮件验证尝试此操作。
public boolean checkemail(String email)
{
Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+");
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}