/* * Copyright 2002-2006,2009 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.opensymphony.xwork2.validator.validators; /** * <!-- START SNIPPET: javadoc --> * EmailValidator checks that a given String field, if not empty, is a valid email address. * * The regular expression used to validate that the string is an email address is: * * <pre> * \\b^['_a-z0-9-\\+]+(\\.['_a-z0-9-\\+]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2}|aero|arpa|asia|biz|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|nato|net|org|pro|tel|travel|xxx)$\\b * </pre> * * You can also specify expression, caseSensitive and trim params as a OGNL expression, see the example below. * <!-- END SNIPPET: javadoc --> * * * <!-- START SNIPPET: parameters --> * <ul> * <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li> * </ul> * Check also documentation of the RegexpValidator for more details - the EmailValidator bases on it. * <!-- END SNIPPET: parameters --> * * <!-- START SNIPPET: parameters-warning --> * Do not use ${regexExpression}, ${caseSensitiveExpression} and ${trimExpression} as an expression as this will turn into infinitive loop! * <!-- END SNIPPET: parameters-warning --> * * <pre> * <!-- START SNIPPET: example --> * <!-- Plain Validator Syntax --> * <validators> * <validator type="email"> * <param name="fieldName">myEmail</param> * <message>Must provide a valid email</message> * </validator> * </validators> * * <!-- Field Validator Syntax --> * <field name="myEmail"> * <field-validator type="email"> * <message>Must provide a valid email</message> * </field-validator> * </field> * * <!-- Field Validator Syntax with expressions --> * <!-- Only available when used with xml based configuration, if you want to have the same * flexibility with annotations use @RegexFieldValidator instead --> * <field name="myEmail"> * <field-validator type="email"> * <param name="regexExpression">${emailPattern}</param> <!-- will be evaluated as: String getEmailPattern() --> * <param name="caseSensitiveExpression">${emailCaseSensitive}</param> <!-- will be evaluated as: boolean getEmailCaseSensitive() --> * <param name="trimExpression">${trimEmail}</param> <!-- will be evaluated as: boolean getTrimEmail() --> * <message>Must provide a valid email</message> * </field-validator> * </field> * <!-- END SNIPPET: example --> * </pre> * * @author jhouse * @author tm_jee * @version $Date$ $Id$ */ public class EmailValidator extends RegexFieldValidator { // see XW-371 public static final String EMAIL_ADDRESS_PATTERN = "\\b^['_a-z0-9-\\+]+(\\.['_a-z0-9-\\+]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2}|aero|arpa|asia|biz|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|nato|net|org|pro|tel|travel|xxx)$\\b"; public EmailValidator() { setRegex(EMAIL_ADDRESS_PATTERN); setCaseSensitive(false); } }