[英]Check if a string only contains letters - Email Validation
I'm new to Android developing. 我是Android开发的新手。 In my login authentication activity, I want validation for input fields.
在我的登录身份验证活动中,我希望验证输入字段。 I want the characters after "."
我想要在“。”之后的字符。 to be letters only.
只能是字母。
ehsan@gmail. ehsan @ gmail。 com
com
If user typed a String like this: ehsan@gmail.23 return false and make a Toast. 如果用户键入如下字符串:ehsan@gmail.23返回false并进行祝酒。 my code is:
我的代码是:
String email = "ehsan@gmail.com";
String[] ar = email.split(".");
if ( !ar[ar.length -1].matches("[a-z]") ){
Toast.makeText(getApplicationContext(), "your email is invalid", Toast.LENGTH_LONG).show();
return false; }
When I run this code, the app force closes. 当我运行此代码时,应用程序强制关闭。 What is wrong?
怎么了?
Please use the Patterns
class to validate the email address: 请使用
Patterns
类来验证电子邮件地址:
public static boolean isValidEmail(String email) {
return !TextUtils.isEmpty(email) && Patterns.EMAIL_ADDRESS.matcher(email).matches();
}
I'm not complitely sure, but I suppose that problem is character @, its not in az interval. 我不确定,但是我想问题出在字符@,它不在z间隔内。 Hence, try
因此,尝试
!ar[ar.length -1].matches("[a-z@]")
Also, keep in mind that e-mail could contain other special characters as '_' for example 另外,请记住,电子邮件可能包含其他特殊字符,例如“ _”
email.matches(".+\\\\.[a-zA-Z]+")
seems to do the job. email.matches(".+\\\\.[a-zA-Z]+")
似乎可以完成工作。 It's not the most sophisticated regex in the world, but it works. 它不是世界上最复杂的正则表达式,但是可以工作。
valid@gmail.com
matches. valid@gmail.com
匹配项。 invalid@gmail.a1b
doesn't match. invalid@gmail.a1b
不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.