[英]Java email and password validation
I am quite new in java programming, ive tackling this problem for almost an hour and i am unsure why my validation for my password keep stating " Passsword contain space" & and my validation for email states " Invalid Email address"我是 Java 编程的新手,我解决了这个问题将近一个小时,但我不确定为什么我的密码验证一直显示“密码包含空格”和我的电子邮件验证状态“无效的电子邮件地址”
ive look throughout my codes several times but i am unable to detect any error.我多次查看我的代码,但我无法检测到任何错误。 Any help would be greatly appreciated.
任何帮助将不胜感激。
public boolean validate() {
if (email == null) {
message = "no email address set";
return false;
}
if (password == null) {
message = "no password set";
return false;
}
if (!email.matches("\\w+@\\.\\+")) {
message = "Invalid Email address";
return false;
}
if (password.length() < 8) {
message = "Password must be at least 8 characters";
return false;
}
//
else if (!password.matches("\\w*\\s+\\w*")) {
message = "Password cannot contain space";
return false;
}
return true;
}
You need to change your below email & password validation:您需要更改以下电子邮件和密码验证:
if (!email.matches("\\w+@\\.\\+")) {
message = "Invalid Email address";
return false;
}
// And below
else if (!password.matches("\\w*\\s+\\w*")) {
message = "Password cannot contain space";
return false;
}
To,到,
public static final Pattern VALID_EMAIL_ADDRESS_REGEX =
Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
public boolean validateEmailId(String emailId) {
Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(emailId);
return matcher.find();
}
public boolean validate() {
//...other conditions as it is
//Invalid Email address
if(!validateEmailId(email)){
message = "Invalid Email address";
return false;
}
//Password cannot contain space
else if(!Pattern.matches("[^ ]*", password)){
message = "Password cannot contain space";
return false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.