Validate an email inside an 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 中的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:验证 EditText 中的电子邮件
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
