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.
2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.validator;
19
import org.apache.oro.text.perl.Perl5Util;
22
* <p>Perform email validations.</p>
24
* This class is a Singleton; you can retrieve the instance via the getInstance() method.
27
* Based on a script by <a href="mailto:stamhankar@hotmail.com">Sandeep V. Tamhankar</a>
28
* http://javascript.internet.com
31
* This implementation is not guaranteed to catch all possible errors in an email address.
32
* For example, an address like nobody@noplace.somedog will pass validator, even though there
36
* @version $Revision: 478560 $ $Date: 2006-11-23 13:09:27 +0000 (Thu, 23 Nov 2006) $
37
* @since Validator 1.1
39
public class EmailValidator {
41
private static final String SPECIAL_CHARS = "\\000-\\037\\(\\)<>@,;:'\\\\\\\"\\.\\[\\]\\177";
42
private static final String VALID_CHARS = "[^\\s" + SPECIAL_CHARS + "]";
43
private static final String QUOTED_USER = "(\"[^\"]*\")";
44
private static final String ATOM = VALID_CHARS + '+';
45
private static final String WORD = "((" + VALID_CHARS + "|')+|" + QUOTED_USER + ")";
47
// Each pattern must be surrounded by /
48
private static final String LEGAL_ASCII_PATTERN = "/^[\\000-\\177]+$/";
49
private static final String EMAIL_PATTERN = "/^(.+)@(.+)$/";
50
private static final String IP_DOMAIN_PATTERN =
51
"/^\\[(\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})[.](\\d{1,3})\\]$/";
52
private static final String TLD_PATTERN = "/^([a-zA-Z]+)$/";
54
private static final String USER_PATTERN = "/^\\s*" + WORD + "(\\." + WORD + ")*$/";
55
private static final String DOMAIN_PATTERN = "/^" + ATOM + "(\\." + ATOM + ")*\\s*$/";
56
private static final String ATOM_PATTERN = "/(" + ATOM + ")/";
59
* Singleton instance of this class.
61
private static final EmailValidator EMAIL_VALIDATOR = new EmailValidator();
64
* Returns the Singleton instance of this validator.
65
* @return singleton instance of this validator.
67
public static EmailValidator getInstance() {
68
return EMAIL_VALIDATOR;
72
* Protected constructor for subclasses to use.
74
protected EmailValidator() {
79
* <p>Checks if a field has a valid e-mail address.</p>
81
* @param email The value validation is being performed on. A <code>null</code>
82
* value is considered invalid.
83
* @return true if the email address is valid.
85
public boolean isValid(String email) {
90
Perl5Util matchAsciiPat = new Perl5Util();
91
if (!matchAsciiPat.match(LEGAL_ASCII_PATTERN, email)) {
95
email = stripComments(email);
97
// Check the whole email address structure
98
Perl5Util emailMatcher = new Perl5Util();
99
if (!emailMatcher.match(EMAIL_PATTERN, email)) {
103
if (email.endsWith(".")) {
107
if (!isValidUser(emailMatcher.group(1))) {
111
if (!isValidDomain(emailMatcher.group(2))) {
119
* Returns true if the domain component of an email address is valid.
120
* @param domain being validatied.
121
* @return true if the email address's domain is valid.
123
protected boolean isValidDomain(String domain) {
124
boolean symbolic = false;
125
Perl5Util ipAddressMatcher = new Perl5Util();
127
if (ipAddressMatcher.match(IP_DOMAIN_PATTERN, domain)) {
128
if (!isValidIpAddress(ipAddressMatcher)) {
134
// Domain is symbolic name
135
Perl5Util domainMatcher = new Perl5Util();
136
symbolic = domainMatcher.match(DOMAIN_PATTERN, domain);
140
if (!isValidSymbolicDomain(domain)) {
151
* Returns true if the user component of an email address is valid.
152
* @param user being validated
153
* @return true if the user name is valid.
155
protected boolean isValidUser(String user) {
156
Perl5Util userMatcher = new Perl5Util();
157
return userMatcher.match(USER_PATTERN, user);
161
* Validates an IP address. Returns true if valid.
162
* @param ipAddressMatcher Pattren matcher
163
* @return true if the ip address is valid.
165
protected boolean isValidIpAddress(Perl5Util ipAddressMatcher) {
166
for (int i = 1; i <= 4; i++) {
167
String ipSegment = ipAddressMatcher.group(i);
168
if (ipSegment == null || ipSegment.length() <= 0) {
175
iIpSegment = Integer.parseInt(ipSegment);
176
} catch(NumberFormatException e) {
180
if (iIpSegment > 255) {
189
* Validates a symbolic domain name. Returns true if it's valid.
190
* @param domain symbolic domain name
191
* @return true if the symbolic domain name is valid.
193
protected boolean isValidSymbolicDomain(String domain) {
194
String[] domainSegment = new String[10];
195
boolean match = true;
197
Perl5Util atomMatcher = new Perl5Util();
199
match = atomMatcher.match(ATOM_PATTERN, domain);
201
domainSegment[i] = atomMatcher.group(1);
202
int l = domainSegment[i].length() + 1;
204
(l >= domain.length())
206
: domain.substring(l);
214
// Make sure there's a host name preceding the domain.
219
// TODO: the tld should be checked against some sort of configurable
221
String tld = domainSegment[len - 1];
222
if (tld.length() > 1) {
223
Perl5Util matchTldPat = new Perl5Util();
224
if (!matchTldPat.match(TLD_PATTERN, tld)) {
234
* Recursively remove comments, and replace with a single space. The simpler
235
* regexps in the Email Addressing FAQ are imperfect - they will miss escaped
236
* chars in atoms, for example.
237
* Derived From Mail::RFC822::Address
238
* @param emailStr The email address
239
* @return address with comments removed.
241
protected String stripComments(String emailStr) {
242
String input = emailStr;
243
String result = emailStr;
244
String commentPat = "s/^((?:[^\"\\\\]|\\\\.)*(?:\"(?:[^\"\\\\]|\\\\.)*\"(?:[^\"\\\\]|\111111\\\\.)*)*)\\((?:[^()\\\\]|\\\\.)*\\)/$1 /osx";
245
Perl5Util commentMatcher = new Perl5Util();
246
result = commentMatcher.substitute(commentPat,input);
247
// This really needs to be =~ or Perl5Matcher comparison
248
while (!result.equals(input)) {
250
result = commentMatcher.substitute(commentPat,input);