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 中的电子邮件
基础教程推荐
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
