litesoft
@ 964
litesoft / trunk / GWT_Sandbox / FormEngine / src / com / temp / common / shared / validators / EmailValidator.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
package com.temp.common.shared.validators; import org.litesoft.externalization.shared.*; import com.temp.common.shared.*; import com.temp.common.shared.utils.*; /** * Email Address Validator, that requires at a minimum that an email address * consists of at least: an addressee, an at sign ('@'), and a domain. * <p/> * Specifically, no spaces or control characters are allowed, addresses can not * be empty, and the domain must consist of at least two levels separated by * periods ('.'). * <p/> * The resulting format for the email is:<br /> * <ul> * (addressee)@[((domain).)*](secondary domain).(primary domain) * </ul> * * @author georgs */ public class EmailValidator implements ValueValidator<String> { public enum ErrorTemplateIdCode { EmailEmpty, // // EmailNoAtSign, // EmailNoSpacesAllowed, // EmailNoCtrlCharsAllowed, // // EmailNoSecondaryDomain, // EmailInvalidDomainFormat, // // EmailNoAddressee, // EmailInvalidAddresseeFormat } public static final EmailValidator INSTANCE = new EmailValidator(); protected EmailValidator() { } public boolean isValid( String email ) { return (null == checkValue( email )); } @Override public E13nData checkValue( String email ) { email = StringUtils.noEmpty( email ); if ( email == null ) { return E13nData.builder( ErrorTemplateIdCode.EmailEmpty ).build(); } int at = email.indexOf( ' ' ); if ( at != -1 ) { return E13nData.builder( ErrorTemplateIdCode.EmailNoSpacesAllowed ).build(); } at = StringUtils.indexOfControlCharacter( email ); if ( at != -1 ) { return E13nData.builder( ErrorTemplateIdCode.EmailNoCtrlCharsAllowed ).build(); } at = email.indexOf( '@' ); if ( at == -1 ) { return E13nData.builder( ErrorTemplateIdCode.EmailNoAtSign ).build(); } String addressee = email.substring( 0, at ); String domain = email.substring( at + 1 ); if ( domain.indexOf( '.' ) == -1 ) { return E13nData.builder( ErrorTemplateIdCode.EmailNoSecondaryDomain ).build(); } if ( !domainOK( domain ) ) { return E13nData.builder( ErrorTemplateIdCode.EmailInvalidDomainFormat ).build(); } if ( addressee.length() == 0 ) { return E13nData.builder( ErrorTemplateIdCode.EmailNoAddressee ).build(); } return addresseeOK( addressee ) ? null : E13nData.builder( ErrorTemplateIdCode.EmailInvalidAddresseeFormat ).build(); } protected boolean domainOK( String domain ) { int from = 0; for ( int at; -1 != (at = domain.indexOf( '.', from )); from = at + 1 ) { if ( !subDomainOK( domain.substring( from, at ) ) ) { return false; } } return subDomainOK( domain.substring( from ) ); } /** * Check that the Sub-Domain is OK. * <p/> * TODO: this should be evolved to have the "real" rules! * * @param subDomain !null */ protected boolean subDomainOK( String subDomain ) { return (subDomain.length() != 0); } /** * Check that the Addressee is OK. * <p/> * TODO: this should be evolved to have the "real" rules! * * @param addressee !null */ protected boolean addresseeOK( String addressee ) { return true; } } |
Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/common/shared/validators/EmailValidator.java
Revision | Author | Commited | Message |
---|---|---|---|
964 | GeorgeS | Fri 01 Aug, 2014 03:18:23 +0000 | ! |