本文介绍了验证电子邮件一个EditText内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证里面一个EditText推出了一个电子邮件,这将code,我已经有:

  textMessage.addTextChangedListener(新TextWatcher(){
        公共无效afterTextChanged(编辑S){
            。如果(textMessage.getText()的toString()匹配([A-ZA-Z0-9 ._-] + @ [AZ] + [AZ] +)及和放大器; s.length()&GT ; 0)
            {
                text.setText(有效的电子邮件);
            }
            其他
            {
                text.setText(无效的电子邮件);
            }
        }
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){}
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){}
    });
 

现在的问题是,当我介绍后,@3个字,它出现的消息有效的电子邮件时,当我介绍了完整的电子邮件就必须出现。

任何suggerence?

感谢大家!

解决方案

只要改变你的正常EX pression如下:

 [A-ZA-Z0-9 ._-] + @ [A-Z] + \\ + [A-Z] +
 

由于。 (点)可以匹配任何单char.ADD一个双反斜线之前,你的圆点代表一个真实的点。

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

    textMessage.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            if (textMessage.getText().toString().matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+") && s.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.

这篇关于验证电子邮件一个EditText内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:20