package com.util;

import java.util.regex.Pattern;

/**
 * 정규식을 이용한 값 체크
 *
 * @author bbaeggar
 *
 */
public class ValidationUtil {

    /**
     * 일반 전화번호 검증
     *
     * @param phoneNum
     * @return
     */
    public static boolean isNomalPhoneNumber(String phoneNum, String separate) {
        String regEx = "^\\d{2,3}"+separate+"\\d{3,4}"+separate+"\\d{4}$";      
        return Pattern.matches(regEx, phoneNum);
    }
  
    /**
     * 모바일 전화번호 검증
     *
     * @param phoneNum
     * @return
     */
    public static boolean isMobilePhoneNumber(String phoneNum, String separate) {
        String regEx = "^01(?:[0-9])"+separate+"(?:\\d{3}|\\d{4})"+separate+"\\d{4}$";      
        return Pattern.matches(regEx, phoneNum);
    }

    /**
     * V4 Ip Address 검증
     *
     * @param ipAddress
     * @return
     */
    public static boolean isIpAdressVersion4(String ipAddress) {
        String regEx = "([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})";      
        return Pattern.matches(regEx, ipAddress);
    }
  
    /**
     * 주민번호 검증
     *
     * @param ipAddress
     * @return
     */
    public static boolean isJuminNumber(String number) {
        String regEx = "\\d{6}\\-[1-4]\\d{6}";      
        return Pattern.matches(regEx, number);
    }

    /**
     * 이메일 검증
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        String regEx = "^([0-9a-zA-Z_-]+)@([0-9a-zA-Z_-]+)(\\.[0-9a-zA-Z_-]+){1,2}$";      
        return Pattern.matches(regEx, email);
    }
  
    public static void main(String[] args) {      
        System.out.println(ValidationUtil.isNomalPhoneNumber("02-123-4516", "-"));
        System.out.println(ValidationUtil.isMobilePhoneNumber("016-0014-1234", "-"));
        System.out.println(ValidationUtil.isIpAdressVersion4("1.1.1.1"));
        System.out.println(ValidationUtil.isJuminNumber("123456-1634316"));
        System.out.println(ValidationUtil.isEmail("bbaeggar@gmail.com"));
    }
}

Posted by 짱가쟁이