验证 EditText 中的电子邮件 [英] Validate an email inside an EditText

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

问题描述

我想验证在 EditText 中引入的电子邮件以及我已经拥有的代码:

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

最终的 EditText textMessage = (EditText)findViewById(R.id.textMessage);

final EditText textMessage = (EditText)findViewById(R.id.textMessage);

最终 TextView 文本 = (TextView)findViewById(R.id.text);

final TextView text = (TextView)findViewById(R.id.text);

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

问题是,当我在@"之后引入 3 个字符时,它会出现消息有效电子邮件",而当我引入完整的电子邮件时它必须出现.

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.

有什么建议吗?

谢谢大家!

推荐答案

只要改变你的正则表达式如下:

Just change your regular expression as follows:

"[a-zA-Z0-9._-]+@[a-z]+\.+[a-z]+"

因为 .(dot) 表示匹配任何单字符.在您的点之前添加一个双反斜杠以代表一个真正的点.

Because . (dot) means match any single-char.ADD a double backslash before your dot to stand for a real dot.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆