Author: sichen
Date: Fri Nov 24 07:55:37 2006 New Revision: 478901 URL: http://svn.apache.org/viewvc?view=rev&rev=478901 Log: UtilValidate.isEmail now has the default isEmail(string) method which accepts formats such as sichen@localhost and isEmail(string, requireDot) which requires formats like [hidden email] Modified: incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilValidate.java Modified: incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilValidate.java URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilValidate.java?view=diff&rev=478901&r1=478900&r2=478901 ============================================================================== --- incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilValidate.java (original) +++ incubator/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilValidate.java Fri Nov 24 07:55:37 2006 @@ -678,40 +678,39 @@ return ((ContiguousUSStateCodes.indexOf(s) != -1) && (s.indexOf(USStateCodeDelimiter) == -1)); } + public static boolean isEmail(String s) { + return isEmail(s, false); + } + /** Email address must be of form [hidden email] -- in other words: * - there must be at least one character before the @ * - there must be at least one character before and after the . - * - the characters @ and . are both required + * - the character @ is required, and . requirement is controlled + * - by requireDot */ - public static boolean isEmail(String s) { + public static boolean isEmail(String s, boolean requireDot) { + + // todo: use regular expression validation + if (isEmpty(s)) return defaultEmptyOK; // is s whitespace? if (isWhitespace(s)) return false; - // there must be >= 1 character before @, so we - // start looking at character position 1 - // (i.e. second character) - int i = 1; - int sLength = s.length(); - - // look for @ - while ((i < sLength) && (s.charAt(i) != '@')) i++; - - // there must be at least one character after the . - if ((i >= sLength - 1) || (s.charAt(i) != '@')) - return false; - else - return true; - - // DEJ 2001-10-13 Don't look for '.', some valid emails do not have a dot in the domain name - // else i += 2; - - // look for . - // while((i < sLength) && (s.charAt(i) != '.')) i++; - // there must be at least one character after the . - // if((i >= sLength - 1) || (s.charAt(i) != '.')) return false; - // else return true; + int atSymbolIndex = s.indexOf('@'); + + // there must be >= 1 character before @ + // indexOf returns -1 if char not found, so 0 or -1 are bad + if (atSymbolIndex <= 0 ) return false; + + if (requireDot) { + int dotIndex = s.lastIndexOf('.'); + if (dotIndex == -1) return false; // no dot + if (dotIndex < atSymbolIndex + 2) return false; // nothing between @ and . + if (dotIndex == s.length() - 1 ) return false; // . is last character + } + + return true; } /** isUrl returns true if the string contains :// |
Free forum by Nabble | Edit this page |