Java电子邮件和密码验证
我是java编程新手,我已经解决这个问题将近一个小时了,我不确定为什么我的密码验证一直声明“Passbrow contain space”&而我的电子邮件验证声明“无效电子邮件地址” 我查看了我的代码好几次,但我没有发现任何错误。任何帮助都将不胜感激Java电子邮件和密码验证,java,regex,Java,Regex,我是java编程新手,我已经解决这个问题将近一个小时了,我不确定为什么我的密码验证一直声明“Passbrow contain space”&而我的电子邮件验证声明“无效电子邮件地址” 我查看了我的代码好几次,但我没有发现任何错误。任何帮助都将不胜感激 public boolean validate() { if (email == null) { message = "no email address set"; return false; }
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;
}
public boolean validate(){
如果(电子邮件==null){
message=“未设置电子邮件地址”;
返回false;
}
如果(密码==null){
message=“未设置密码”;
返回false;
}
如果(!email.matches(\\w++\\.\\+')){
message=“无效的电子邮件地址”;
返回false;
}
if(password.length()<8){
message=“密码必须至少为8个字符”;
返回false;
}
//
如果(!password.matches(\\w*\\s+\\w*'),则为else{
message=“密码不能包含空格”;
返回false;
}
返回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;
}
}