如何在中執行Email Validation
android
?我已經通過谷歌& SO,但我沒有找到一個簡單的方法來驗證它。Android上的EditText上的電子郵件地址驗證
回答
要執行電子郵件驗證我們有很多方法,但簡單的&最簡單的方法是兩種方法。
1-使用EditText(....).addTextChangedListener
其一直在EditText box
即EMAIL_ID上的每個輸入觸發是無效或有效使用if-else
條件
/**
* Email Validation ex:- [email protected]
*/
final EditText emailValidate = (EditText)findViewById(R.id.textMessage);
final TextView textView = (TextView)findViewById(R.id.text);
String email = emailValidate.getText().toString().trim();
String emailPattern = "[a-zA-Z0-9._-][email protected][a-z]+\\.+[a-z]+";
emailValidate .addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (email.matches(emailPattern) && s.length() > 0)
{
Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
// or
textView.setText("valid email");
}
else
{
Toast.makeText(getApplicationContext(),"Invalid email address",Toast.LENGTH_SHORT).show();
//or
textView.setText("invalid email");
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// other stuffs
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// other stuffs
}
});
2-簡單的方法。使用getText()獲取EditText框字符串,並與爲電子郵件提供的模式進行比較。如果模式不匹配或macthes,onClick按鈕烤麪包消息。它不會觸發EditText框中每個字符的輸入。簡單的例子如下所示。
final EditText emailValidate = (EditText)findViewById(R.id.textMessage);
final TextView textView = (TextView)findViewById(R.id.text);
String email = emailValidate.getText().toString().trim();
String emailPattern = "[a-zA-Z0-9._-][email protected][a-z]+\\.+[a-z]+";
// onClick of button perform this simplest code.
if (email.matches(emailPattern))
{
Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"Invalid email address", Toast.LENGTH_SHORT).show();
}
使用此方法驗證您的電子郵件格式。將電子郵件作爲字符串傳遞,如果格式正確則返回true,否則返回false。
/**
* validate your email address format. [email protected]
*/
public boolean emailValidator(String email)
{
Pattern pattern;
Matcher matcher;
final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(email);
return matcher.matches();
}
感謝您的建議.. –
試試這個:
if (!emailRegistration.matches("[a-zA-Z0-9._-][email protected][a-z]+.[a-z]+")) {
editTextEmail.setError("Invalid Email Address");
}
試試這個
public static final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
"\\@" +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
"(" +
"\\." +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
")+"
);
和TNE編輯文本
final String emailText = email.getText().toString();
EMAIL_ADDRESS_PATTERN.matcher(emailText).matches()
感謝您的建議。 –
這是Android模式,並接受[email protected]。你有關於這個場景的任何解決方案 – theLazyFinder
public static boolean isEmailValid(String email) {
boolean isValid = false;
String expression = "^[\\w\\.-][email protected]([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
感謝您的建議.. –
這工作完美的我,可能是表達使差異.. –
這是一個示例我thod我創建驗證電子郵件地址,如果傳遞的字符串參數是一個有效的電子郵件地址,它返回true,否則返回false。
private boolean validateEmailAddress(String emailAddress){
String expression="^[\\w\\-]([\\.\\w])+[\\w][email protected]([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = emailAddress;
Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
return matcher.matches();
}
謝謝您的建議.. –
@TechEnd歡迎 – AppMobiGurmeet
這太容易了:
添加此方法電子郵件地址到檢查是否有效或不:
private boolean isValidEmaillId(String email){
return Pattern.compile("^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
+ "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
+ "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$").matcher(email).matches();
}
現在用字符串檢查EditText上 的:
if(isValidEmaillId(edtEmailId.getText().toString().trim())){
Toast.makeText(getApplicationContext(), "Valid Email Address.", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "InValid Email Address.", Toast.LENGTH_SHORT).show();
}
完成
public final static boolean isValidEmail(CharSequence target) {
return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches());
}
編輯::它將工作在Android 2.2+起! 編輯:添加缺少;
使用此方法來驗證EMAIL: -
public static boolean isEditTextContainEmail(EditText argEditText) {
try {
Pattern pattern = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
Matcher matcher = pattern.matcher(argEditText.getText());
return matcher.matches();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
讓我知道如果您有任何疑問?
- 1. EditText上的電子郵件驗證 - Android
- 2. ASP.NET的電子郵件地址驗證
- 3. 如何驗證CSV文件上的電子郵件地址:: laravel
- 4. 使用javascript&jquery驗證按鍵上的電子郵件地址
- 5. URL上的驗證電子郵件地址
- 6. 驗證ASP.NET C#上的自定義ac.uk地址的電子郵件地址
- 7. OSCLASS:驗證電子郵件地址
- 8. 驗證電子郵件地址
- 9. 電子郵件地址輸入驗證
- 10. ZF2電子郵件地址驗證器
- 11. 電子郵件地址驗證狀態
- 12. 驗證電子郵件地址
- 13. iOS電子郵件地址驗證
- 14. ListView驗證電子郵件地址
- 15. 驗證電子郵件地址(域名)
- 16. 在Dart驗證電子郵件地址?
- 17. 電子郵件地址驗證
- 18. 如何驗證電子郵件地址
- 19. 電子郵件地址驗證,如何?
- 20. 如何驗證電子郵件地址
- 21. C#電子郵件地址驗證
- 22. 驗證電子郵件地址失敗
- 23. 目標電子郵件地址驗證
- 24. 電子郵件驗證裏面的EditText
- 25. 在交換服務器上驗證電子郵件地址
- 26. 如何在php腳本上驗證電子郵件地址
- 27. 窗體上的電子郵件驗證
- 28. 重寫jQuery驗證插件電子郵件地址驗證
- 29. Android電子郵件驗證與Firebase電子郵件驗證
- 30. 正則表達式問題 - 驗證電子郵件地址 - Android
android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches(); –