我有一个Email EditText,我想通过电子邮件验证来检查它。
这是我的电子邮件验证码
public final static boolean isValidEmail(CharSequence target) {
if (target == null) {
return false;
} else {
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
}
public void showAlertValidation() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(RegisterActivity.this);
alertDialog.setTitle("Failed");
alertDialog.setMessage("Invalid Email");
alertDialog.setNegativeButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
这是我的EditText验证码
editTextEmail= (EditText) findViewById(R.id.editTextEmail);
email = editTextEmail.getText().toString();
if(email.length() == 0) {
editTextEmail.setError("Email required!");
if (isValidEmail(email)) {
Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
}
else{
showAlertValidation();
}
}
问题是EditText的结果。当EditText的值为null时,它将运行showAlertValidation();
但是如果EditText的值是"电子邮件"或者"发送电子邮件@ example"或" email@example.com" ;,它没有运行showAlertValidation();
我的代码出了什么问题?
答案 0 :(得分:11)
工作!!!!
我们现在有简单的电子邮件模式匹配器
private static boolean isValidEmail(String email) {
return !TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
答案 1 :(得分:1)
您错误地将EditText
文本长度与0进行比较,并且只有在它为真的情况下,才会执行验证逻辑。
这是正确的代码:
@Override
public void onClick(View v) {
String email = editTextEmail.getText().toString();
if(email.length() != 0) {
if (isValidEmail(email)) {
Toast.makeText(getApplicationContext(), "Valid email address!", Toast.LENGTH_SHORT).show();
}
else{
editTextEmail.setError("Email required!");
showAlertValidation();
}
}
else{
editTextEmail.setError("Email required!");
}
}
答案 2 :(得分:1)
最好创建一个验证电子邮件的类。这样您就可以在任何需要的地方重复使用它。您甚至不需要将edittext的文本放在单独的字符串中。制作以下课程:
public class EmailValidator {
private Pattern pattern;
private Matcher matcher;
private static EmailValidator sInstance;
public static EmailValidator getInstance() {
if (sInstance == null) {
sInstance = new EmailValidator();
}
return sInstance;
}
private static final String EMAIL_PATTERN =
"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
public EmailValidator() {
pattern = Pattern.compile(EMAIL_PATTERN);
}
public boolean validate(final String hex) {
matcher = pattern.matcher(hex);
return matcher.matches();
}}
现在使用此类检查edittext的电子邮件验证,如下所示:
if(!EmailValidator.getInstance().validate(editTextEmail.getText().toString().trim())){
editTextEmail.setError("Invalid email address");
}
希望这有帮助。
答案 3 :(得分:0)
我认为这是你犯了错误的地方(EditText验证码):
editTextEmail= (EditText) findViewById(R.id.editTextEmail);
email = editTextEmail.getText().toString();
if(email.length() == 0) {
editTextEmail.setError("Email required!");
}
else if (isValidEmail(email)) {
Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
}
else{
showAlertValidation();
}
答案 4 :(得分:0)
在使用String.trim();
检查电子邮件字符串的长度之前,您应该清空空格“”if(email.trim().length() == 0) {//code here}
答案 5 :(得分:0)
问题是你的条件
if(email.length() == 0)
{
editTextEmail.setError("Email required!");
if (isValidEmail(email))
{
Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
}
else
{
showAlertValidation();
}
}
因为它只在您的editText为空时验证您的电子邮件地址。 你能做的是:
email.addTextChangedListener(new TextWatcher()
{
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
@Override
public void afterTextChanged(Editable s)
{
if(s.length() > 0)
{
if (isValidEmail(email))
{
editTextEmail.setError(null);
Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"valid email address Required",Toast.LENGTH_SHORT).show();
}
}
}
});