2
* $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/EmailValidator.java,v 1.12 2004/02/21 17:10:29 rleland Exp $
4
* $Date: 2004/02/21 17:10:29 $
6
* ====================================================================
7
* Copyright 2001-2004 The Apache Software Foundation
9
* Licensed under the Apache License, Version 2.0 (the "License");
10
* you may not use this file except in compliance with the License.
11
* You may obtain a copy of the License at
13
* http://www.apache.org/licenses/LICENSE-2.0
15
* Unless required by applicable law or agreed to in writing, software
16
* distributed under the License is distributed on an "AS IS" BASIS,
17
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
* See the License for the specific language governing permissions and
19
* limitations under the License.
22
package org.apache.commons.validator;
24
import org.apache.oro.text.perl.Perl5Util;
27
* <p>Perform email validations.</p>
29
* This class is a Singleton; you can retrieve the instance via the getInstance() method.
32
* Based on a script by <a href="mailto:stamhankar@hotmail.com">Sandeep V. Tamhankar</a>
33
* http://javascript.internet.com
36
* @since Validator 1.1
38
public class EmailValidator {
40
private static final String SPECIAL_CHARS = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
41
private static final String VALID_CHARS = "[^\\s" + SPECIAL_CHARS + "]";
42
private static final String QUOTED_USER = "(\"[^\"]*\")";
43
private static final String ATOM = VALID_CHARS + '+';
44
private static final String WORD = "(" + ATOM + "|" + QUOTED_USER + ")";
46
// Each pattern must be surrounded by /
47
private static final String LEGAL_ASCII_PATTERN = "/^[\\000-\\177]+$/";
48
private static final String EMAIL_PATTERN = "/^(.+)@(.+)$/";
49
private static final String IP_DOMAIN_PATTERN =
50
"/^(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})$/";
52
private static final String USER_PATTERN = "/^" + WORD + "(\\." + WORD + ")*$/";
53
private static final String DOMAIN_PATTERN = "/^" + ATOM + "(\\." + ATOM + ")*$/";
54
private static final String ATOM_PATTERN = "/(" + ATOM + ")/";
57
* Singleton instance of this class.
59
private static final EmailValidator instance = new EmailValidator();
62
* Returns the Singleton instance of this validator.
64
public static EmailValidator getInstance() {
69
* Protected constructor for subclasses to use.
71
protected EmailValidator() {
76
* <p>Checks if a field has a valid e-mail address.</p>
78
* @param email The value validation is being performed on. A <code>null</code>
79
* value is considered invalid.
81
public boolean isValid(String email) {
86
Perl5Util matchAsciiPat = new Perl5Util();
87
if (!matchAsciiPat.match(LEGAL_ASCII_PATTERN, email)) {
91
// Check the whole email address structure
92
Perl5Util emailMatcher = new Perl5Util();
93
if (!emailMatcher.match(EMAIL_PATTERN, email)) {
97
if (email.endsWith(".")) {
101
if (!isValidUser(emailMatcher.group(1))) {
105
if (!isValidDomain(emailMatcher.group(2))) {
113
* Returns true if the domain component of an email address is valid.
114
* @param domain being validatied.
116
protected boolean isValidDomain(String domain) {
117
boolean symbolic = false;
118
Perl5Util ipAddressMatcher = new Perl5Util();
120
if (ipAddressMatcher.match(IP_DOMAIN_PATTERN, domain)) {
121
if (!isValidIpAddress(ipAddressMatcher)) {
125
// Domain is symbolic name
126
Perl5Util domainMatcher = new Perl5Util();
127
symbolic = domainMatcher.match(DOMAIN_PATTERN, domain);
131
if (!isValidSymbolicDomain(domain)) {
142
* Returns true if the user component of an email address is valid.
143
* @param user being validated
145
protected boolean isValidUser(String user) {
146
Perl5Util userMatcher = new Perl5Util();
147
return userMatcher.match(USER_PATTERN, user);
151
* Validates an IP address. Returns true if valid.
152
* @param ipAddressMatcher Pattren matcher
154
protected boolean isValidIpAddress(Perl5Util ipAddressMatcher) {
155
for (int i = 1; i <= 4; i++) {
156
String ipSegment = ipAddressMatcher.group(i);
157
if (ipSegment == null || ipSegment.length() <= 0) {
164
iIpSegment = Integer.parseInt(ipSegment);
165
} catch(NumberFormatException e) {
169
if (iIpSegment > 255) {
178
* Validates a symbolic domain name. Returns true if it's valid.
179
* @param domain symbolic domain name
181
protected boolean isValidSymbolicDomain(String domain) {
182
String[] domainSegment = new String[10];
183
boolean match = true;
185
Perl5Util atomMatcher = new Perl5Util();
188
match = atomMatcher.match(ATOM_PATTERN, domain);
190
domainSegment[i] = atomMatcher.group(1);
191
int l = domainSegment[i].length() + 1;
193
(l >= domain.length())
195
: domain.substring(l);
202
if (domainSegment[len - 1].length() < 2
203
|| domainSegment[len - 1].length() > 4) {
208
// Make sure there's a host name preceding the domain.