我是Java编程的新手,我将这个问题解决了将近一个小时,而且我不确定为什么我对密码的验证会一直显示“密码包含空格”和我对电子邮件的验证状态为“无效的电子邮件地址”
香港专业教育学院遍历我的代码几次,但我无法检测到任何错误。任何帮助将不胜感激。
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;
}
您需要更改以下电子邮件和密码验证:
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;
}
至,
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;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句