KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > util > typevalidator > EmailTypeValidator


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.util.typevalidator;
8
9
10 import java.util.Locale JavaDoc;
11
12
13 /**
14  * <p>
15  * This class validates email addresses
16  * </p>
17  *
18  * @author bpontarelli
19  * @version 2.0
20  * @since 2.0
21  */

22 public class EmailTypeValidator extends BaseTypeValidator {
23
24     /**
25      * Default error message returned from validate methods (inside Basic and
26      * PropertyError objects)
27      */

28     public static final String JavaDoc DEFAULT_MESSAGE = "Invalid email address";
29
30
31     /**
32      * Constructs a new <code>EmailTypeValidator</code>
33      */

34     public EmailTypeValidator() {
35         // Empty
36
}
37
38
39     /**
40      * Does the work of validating the email address
41      *
42      * @param value The email address to validate
43      * @param params Not used
44      * @param message The error message to use
45      * @param locale Not used
46      * @param mesgParams Parameters used to format the error message
47      * @return The error message if the validation failed, otherwise null
48      */

49     protected String JavaDoc internalValidate(Object JavaDoc value, Object JavaDoc params, String JavaDoc message,
50             Locale JavaDoc locale, Object JavaDoc[] mesgParams) {
51         String JavaDoc localEmail = value.toString().trim();
52         int at = localEmail.indexOf("@");
53         int space = localEmail.indexOf(" ");
54         boolean valid = (at != -1 && at != 0 &&
55             at != (localEmail.length() - 1) && space == -1);
56         if (valid) {
57             valid = (localEmail.indexOf("@", at + 1) == -1);
58         }
59
60         String JavaDoc error = null;
61         if (!valid) {
62             error = getErrorMessage(message, DEFAULT_MESSAGE, mesgParams);
63         }
64
65         return error;
66     }
67 }
Popular Tags