验证 EditText 中的电子邮件

Validate an email inside an EditText(验证 EditText 中的电子邮件)
本文介绍了验证 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 中的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
cocos2d-android: how to display score(cocos2d-android:如何显示分数)
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
Android file copy(安卓文件拷贝)
Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)