Last active
August 29, 2015 14:04
-
-
Save up1/ee9dd14e4c79b16c549a to your computer and use it in GitHub Desktop.
Demo :: Validation input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EmailFormatRule implements RegisterRule { | |
public void validate(User user) { | |
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; | |
Pattern validEmailPattern = Pattern.compile(EMAIL_PATTERN); | |
if( !validEmailPattern.matcher(user.getEmail()).matches() ) { | |
throw new IllegalArgumentException("Email is wrong format"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NameEmptyRule implements RegisterRule { | |
public void validate(User user) { | |
if (user.getName() == null || user.getName().trim().equals("")) { | |
throw new IllegalArgumentException("Name should not empty"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface RegisterRule { | |
void validate(User user); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean register(User user) { | |
List<RegisterRule> registerRules = new ArrayList<RegisterRule>(); | |
registerRules.add(new NameEmptyRule()); | |
registerRules.add(new NameFormatRule()); | |
registerRules.add(new EmailEmptyRule()); | |
registerRules.add(new EmailFormatRule()); | |
registerRules.add(new EmailDomainRule()); | |
registerRules.add(new AgeAllowRule()); | |
for (RegisterRule registerRule : registerRules) { | |
registerRule.validate(user); | |
} | |
return true; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RegisterService { | |
public boolean register(User user) { | |
for (RegisterRule registerRule : RuleFactory.getRegisterRules()) { | |
registerRule.validate(user); | |
} | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class RuleFactory { | |
private RuleFactory() { | |
} | |
public static List<RegisterRule> getRegisterRules() { | |
List<RegisterRule> registerRules = new ArrayList<RegisterRule>(); | |
registerRules.add(new NameEmptyRule()); | |
registerRules.add(new NameFormatRule()); | |
registerRules.add(new EmailEmptyRule()); | |
registerRules.add(new EmailFormatRule()); | |
registerRules.add(new EmailDomainRule()); | |
registerRules.add(new AgeAllowRule()); | |
return registerRules; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean register(User user) { | |
if (user.getName() == null || user.getName().trim().equals("")) { | |
throw new IllegalArgumentException("Name should not empty"); | |
} | |
if (user.getEmail() == null || user.getEmail().trim().equals("")) { | |
throw new IllegalArgumentException("Email should not empty"); | |
} | |
if ( !user.getName().matches("[a-zA-Z]+")){ | |
throw new IllegalArgumentException("Name is wrong format"); | |
} | |
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; | |
Pattern validEmailPattern = Pattern.compile(EMAIL_PATTERN); | |
if( !validEmailPattern.matcher(user.getEmail()).matches() ) { | |
throw new IllegalArgumentException("Email is wrong format"); | |
} | |
List<String> notAllowDomains = Arrays.asList("domain01.cc","domain02.cc", "domain03.cc"); | |
if(notAllowDomains.contains(user.getEmail().split("@")[1])) { | |
throw new IllegalArgumentException("Domain Email is not allow"); | |
} | |
if( user.getAge() < 20 ) { | |
throw new IllegalArgumentException("Age should more than 20 years"); | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment