問題描述
我想驗證在 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 中的電子郵件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!