Hi
 
I was facing problem in validating multiple Email Addresses given in a form field. As the validator-rules.xml in Validator Framework contains validation for only single Email Address. I tried to modify the code of the existing Email Validator in validator-rules.xml, but it didn't work.
 
So, I created my own java class which validates single/multiple Email Addresses and made the corresponding entry in validator-rules.xml and consequently in validation.xml. Now its working as per requirement.
 
I am giving the java code for that class(CustomValidatorRules.java attached with this mail). Currently in my code the email addresses should be separated by either , or ;. You can modify it for any separator.
 
Pls suggest any modifications in the code given by me, as well as if there is any way to modify the existing code of the Email Validator in validator-rules.xml so that it can validate multiple email addresses.
 
Thanks
Hemant


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Hemant Kumar Sharma
accenture
High Performance.Delivered
Accenture Services Pvt. Ltd.
India Delivery Centre - Mumbai

Direct   : + 91 2255003890 
Mobile  : + 91 9820238651
e-mail   : [EMAIL PROTECTED] , [EMAIL PROTECTED]
AOL IM : hemant2913 , YahooMsngr : hemant2911


Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
/**
 * author hemant.sharma   [EMAIL PROTECTED]
 * Created on Jan 3, 2005
 */

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.ValidatorUtil;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.validator.Resources;


public class CustomValidatorRules {

        public static boolean validateEmailAddresses(
                Object bean,
                ValidatorAction va,
                Field field,
                ActionErrors errors,
                HttpServletRequest request) {

                //Email Addresses obtained from the input form 
                String emailAddresses =
                        ValidatorUtil.getValueAsString(bean, 
field.getProperty());

                List emailAddressList = new ArrayList();
                char[] eAdd = emailAddresses.toCharArray();
                char c;
                int flag = 0;
                int i = 0;
                int count1 = 0, count2 = 0;

                //Check to see if either a semicolon or comma is used between 
email addresses. 
                for (i = 0; i < eAdd.length; i++) {
                        c = eAdd[i];
                        if (c == ',') {
                                count1++;
                        }
                        if (c == ';') {
                                count2++;
                        }

                        if (count1 > 0 && count2 > 0) {
                                errors.add(
                                        field.getKey(),
                                        Resources.getActionError(request, va, 
field));
                                return false;
                        }

                }

                //Check to see if there are either more than one semicolon or 
more than one comma used between email addresses.  
                for (i = 0; i < eAdd.length; i++) {
                        c = eAdd[i];
                        if (c == ',' || c == ';') {
                                if ((i + 1) < eAdd.length
                                        && (eAdd[i + 1] == ',' || eAdd[i + 1] 
== ';')) {

                                        errors.add(
                                                field.getKey(),
                                                
Resources.getActionError(request, va, field));
                                        return false;
                                }
                        }
                }

                //The whole Email Addresses String is tokenized as list of 
email addresses.
                if (flag != 1) {
                        StringTokenizer st = new 
StringTokenizer(emailAddresses, ",;");
                        while (st.hasMoreTokens()) {
                                emailAddressList.add(st.nextToken().trim());
                        }

                        for (int j = 0; j < emailAddressList.size(); j++) {
                                if ((String) emailAddressList.get(j) != null) {
                                        //Each Email Address is checked for 
validity    
                                        if (!isValidEmail((CharSequence) 
emailAddressList
                                                .get(j))) {
                                                //Invalid Email
                                                errors.add(
                                                        field.getKey(),
                                                        
Resources.getActionError(request, va, field));
                                                return false;
                                        }
                                }
                        }

                        //Valid Email
                        return true;

                } else {

                        errors.add(
                                field.getKey(),
                                Resources.getActionError(request, va, field));
                        return false;
                }

        }

        public static boolean isValidEmail(CharSequence emailAddress) {

                CharSequence userPart = null;
                CharSequence domainPart = null;
                int flag = 0;

                /**
                 * Tokenizing the Email Address by 
                 */
                StringTokenizer st = new 
StringTokenizer(emailAddress.toString(), "@");
                List emailAddressPartsList = new ArrayList();
                while (st.hasMoreTokens()) {
                        emailAddressPartsList.add(st.nextToken());
                }

                /*********************/
                Pattern startP = Pattern.compile("^\\@");
                Matcher startM = startP.matcher(emailAddress);
                if (startM.find()) {
                        //Invalid Email : starting with @ 
                        return false;
                }

                /*********************/
                startP = Pattern.compile("^www\\.");
                startM = startP.matcher(emailAddress);
                if (startM.find()) {
                        //Invalid Email : starting with www. 
                        return false;
                }

                /*********************/
                Pattern midP = Pattern.compile("[EMAIL PROTECTED]");
                Matcher midM = midP.matcher(emailAddress);
                if (midM.find()) {
                        //Invalid Email : '.' after @ 
                        return false;
                }

                /*********************/
                midP = Pattern.compile("[EMAIL PROTECTED]@");
                midM = midP.matcher(emailAddress);
                if (midM.find()) {
                        //Invalid Email : @ after @ 
                        return false;
                }

                /*******************/
                Pattern endP = Pattern.compile("[\\.]$");
                Matcher endM = endP.matcher(emailAddress);
                if (endM.find()) {
                        //Invalid Email : ending with '.' 
                        return false;
                }

                /********************/
                if ((emailAddressPartsList.size() > 2)
                        || (emailAddressPartsList.size() == 1)) {
                        //Invalid Email : contains more than one @ or no @
                        return false;
                } else {
                        userPart = (CharSequence) emailAddressPartsList.get(0);
                        domainPart = (CharSequence) 
emailAddressPartsList.get(1);
                        //userPart : " + userPart + "           domainPart : " 
+ domainPart
                }

                /*******************/
                if (userPart != null && userPart.toString().trim().length() != 
0) {

                        Pattern userP =
                                
Pattern.compile("^[a-zA-Z0-9][\\w\\.\\-]+[a-zA-Z0-9]$");
                        Matcher userM = userP.matcher(userPart);
                        if (!userM.find()) {
                                //Invalid Email : userPart didn't match the 
userPattern
                                return false;
                        }
                }

                /*******************/
                if (domainPart != null && domainPart.toString().trim().length() 
!= 0) {
                        StringTokenizer stDomain =
                                new StringTokenizer(domainPart.toString(), ".");
                        List domainPartsList = new ArrayList();
                        while (stDomain.hasMoreTokens()) {
                                domainPartsList.add(stDomain.nextToken());
                        }
                        if (domainPartsList.size() == 1) {
                                //Invalid Email : domainPart didn't contain '.'
                                return false;
                        }
                        Pattern domainP =
                                
Pattern.compile("^[a-zA-Z0-9][\\w\\-]*[a-zA-Z0-9]$");
                        for (int i = 0; i < domainPartsList.size(); i++) {

                                CharSequence domainPartlet =
                                        (CharSequence) domainPartsList.get(i);
                                Matcher domainM = 
domainP.matcher(domainPartlet);
                                if (!domainM.find()) {
                                        //Invalid Email : domainPart didn't 
match the domainPattern
                                        return false;
                                }
                        }
                }

                //Valid Email
                return true;

        }

}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to