Validator for Zip code, Email, Phone number : Email « Network Protocol « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Network Protocol » Email 




Validator for Zip code, Email, Phone number
 
//package com.sybrix.easygsp.util;

import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: dsmith
 * Date: Apr 27, 2007
 * Time: 6:47:36 PM
 *
 * this is not mine
 */
public class Validator {

        private static final String sp = "\\!\\#\\$\\%\\&\\'\\*\\+\\-\\/\\=\\?\\^\\_\\`\\{\\|\\}\\~";
        private static final String atext = "[a-zA-Z0-9" + sp + "]";
        private static final String atom = atext + "+"//one or more atext chars
        private static final String dotAtom = "\\." + atom;
        private static final String localPart = atom + "(" + dotAtom + ")*"//one atom followed by 0 or more dotAtoms.

        //RFC 1035 tokens for domain names:
        private static final String letter = "[a-zA-Z]+$";
        private static final String domainLetter = "[a-zA-Z]+";
        private static final String letDig = "[a-zA-Z0-9]+$";
        private static final String letDigHyp = "[a-zA-Z0-9-]+$";
        private static final String digit = "[0-9]";

        public static final String rfcLabel = "[a-zA-Z0-9]+" "[a-zA-Z0-9-]+" "{0,61}" "[a-zA-Z0-9]+";

        private static final String domain = rfcLabel + "(\\." + rfcLabel + ")*\\." + domainLetter + "{2,6}";
        //Combined together, these form the allowed email regexp allowed by RFC 2822:
        private static final String addrSpec = "^" + localPart + "@" + domain + "$";

        //now compile it:
        public static final Pattern EMAIL_PATTERN = Pattern.compile(addrSpec);

        public static final String phoneNumberRegEx = "(\\d-)?(\\d{3}-)?\\d{3}-\\d{4}";
        public static final Pattern PHONE_PATTERN = Pattern.compile(phoneNumberRegEx);

        public static final String zipCodeRegEx = "\\d{5}(-\\d{4})?";
        public static final Pattern ZIPCODE_PATTERN = Pattern.compile(zipCodeRegEx);

        public static final Pattern ALPHA_NUMERIC_PATTERN = Pattern.compile(letDig);
        public static final Pattern LETTERS_PATTERN = Pattern.compile(letter);
        public static final Pattern DIGIT_PATTERN = Pattern.compile("(\\d+?)");


        public static boolean isEmpty(Object value) {
                if (value == null) {
                        return true;
                else if (value.toString().trim().length() == 0) {
                        return true;
                }
                return false;
        }

        public static boolean isEmailValid(String value) {
                return EMAIL_PATTERN.matcher(value).matches();
        }

        public static boolean isNumeric(String value) {
                if (value == null)
                        return false;
                return DIGIT_PATTERN.matcher(value).matches();
        }

        public static boolean isLettersOnly(String value) {
                if (value == null)
                        return false;
                return LETTERS_PATTERN.matcher(value).matches();
        }

        public static boolean isAlphaNumeric(String value) {
                return ALPHA_NUMERIC_PATTERN.matcher(value).matches();
        }

        public static boolean isValidPhone(String value) {
                return PHONE_PATTERN.matcher(value).matches();
        }

        public static boolean isZipCodeValid(String value) {
                return ZIPCODE_PATTERN.matcher(value).matches();
        }

        public static boolean matches(String value1, String value2) {
                try {
                        if (value1 == null || value2 == null) {
                                return false;
                        else if (value1.trim().length() == || value2.trim().length() == 0) {
                                return false;
                        else {
                                return value1.equals(value2);
                        }
                catch (Exception e) {
                        return false;
                }
        }

        /**
         * Returns -1 if too short, 1 if too long, else 0 if ok
         *
         @param value value to measure
         @param min   - Minimum string length(must be greater than)
         @param max   - Maximum string length (cannot be greater than)
         @return -1 if too short, 1 if too long, else 0 if ok
         */
        public static int lengthMinMax(String value, int min, int max) {
                if (null == value) {
                        return -1;
                else if (value.trim().length() < min) {
                        return -1;
                else if (value.trim().length() > max) {
                        return 1;
                else {
                        return 0;
                }
        }

        public static boolean isTooShort(String value, int min) {
                if (null == value) {
                        return true;
                else if (value.trim().length() < min) {
                        return true;
                else {
                        return false;
                }
        }

        public static boolean isTooLong(String value, int max) {
                if (null == value) {
                        return false;
                else if (value.trim().length() > max) {
                        return true;
                else {
                        return false;
                }
        }

//        public static boolean isCreditCardValid(Object value) {
//                return true;
//        }
//
//        public static boolean isValidUrlValid(Object value) {
//                return true;
//        }

        public static String escapeXML(String s){
                return s.replaceAll("&""&amp;").replaceAll("<""&lt;").replaceAll(">""&gt;");
        }
}

   
  














Related examples in the same category
1.Pure Java Email client
2.Sending Mail Using Sockets
3.Sending Mail
4.Get Email Message Example
5.A Client to Send SMTP MailA Client to Send SMTP Mail
6.Mailer: Sends an email message
7.TestOpenMailRelay -- send self-returning SPAM to check for relay sitesTestOpenMailRelay -- send self-returning SPAM to check for relay sites
8.Sender -- send an email message
9.Sender -- send an email message with attachment
10.SendMime -- send a multi-part MIME email message
11.Read a file return mail headers one at a time
12.Send mail using GMAIL
13.Send Mail Implementation using simple SMTP
14.Send email out
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.