DBILITY

vaadin field validation 실습2 본문

front-end & ui/vaadin legacy

vaadin field validation 실습2

DBILITY 2018. 6. 16. 12:55
반응형

vaadin에서 지원하는 validator를 사용하지 않고, Regular Expression으로 해봤다.

 

 

  • Constants.java

    package com.vseminar.vaadin_seminar2.util;
    
    /**
    *
    * Description
    *
    *
    * @author hyperrookie@gmail.com
    *
    * @version 1.0.0
    * @date 2018. 6. 16.
    *=======================================================================
    * Date            Name                     Revision History
    *=======================================================================
    * 2018. 6. 16.    hyperrookie@gmail.com    Creation
    *=======================================================================
    */
    public class Constants {
    	/*
    	 * https://regexr.com/에서 표현식을 입력하면 실시간으로 설명을 볼 수 있음.
    	 *
    	 * 숫자/대소문자/특수문자 각 1개 포함 최소 8자리
    	 * 	:	^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$
    	 * 숫자/대문자/소문자 각 1개 포함 최소 8자리
    	 * 	:	^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$
    	 * 숫자/대문자/소문자/특수문자 각 1개 포함 최소 8자리
    	 * 	:	^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}$
    	 * 숫자만		:	^[0-9]*$
    	 * 영문자만	:	^[a-zA-Z]*$
    	 * 한글만		:	^[가-힣]*$
    	 * 영어 & 숫자만	:	^[a-zA-Z0-9]*$
    	 * E-Mail	:	^([a-zA-Z0-9_\\.\\-+])+@(([a-zA-Z0-9-])+\\.)+([a-zA-Z0-9]{2,4})+$
    	 * 휴대폰		:	^01(?:0|1|[6-9]) - (?:\d{3}|\d{4}) - \d{4}$
    	 * 일반전화	:	^\d{2.3} - \d{3,4} - \d{4}$
    	 * 주민등록번호:	\d{6} \- [1-4]\d{6}
    	 * IP 주소	:	([0-9]{1,3}) \. ([0-9]{1,3}) \. ([0-9]{1,3}) \. ([0-9]{1,3})
    	 *
    	 */
    	public static final String REGEX_PASSWORD	= "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&])[A-Za-z\\d$@$!%*?&]{8,}$";
    	public static final String REGEX_NUMERIC	= "^[0-9]*$";
    	public static final String REGEX_FLOAT		= "^[+-]?\\d*(\\.?\\d*)$";
    	public static final String REGEX_ALPHA		= "^[a-zA-Z]*$";
    	public static final String REGEX_ALPHA_LOWER	= "^[a-z]*$";
    	public static final String REGEX_ALPHA_UPPER	= "^[A-Z]*$";
    	public static final String REGEX_ALPHANUMERIC	= "^[a-zA-Z0-9]*$";
    	public static final String REGEX_KOREAN	= "^[가-힣]*$";
    	public static final String REGEX_ID		="^[a-zA-Z]{1}[a-zA-Z0-9_]{4,20}$";
    	public static final String REGEX_EMAIL		= "^([a-zA-Z0-9_\\.\\-+])+@(([a-zA-Z0-9-])+\\.)+([a-zA-Z0-9]{2,4})+$";
    	public static final String REGEX_CELLULAR	= "^01(?:0|1|[6-9]) - (?:\\d{3}|\\d{4}) - \\d{4}$";
    	public static final String REGEX_PHONE		= "^\\d{2.3} - \\d{3,4} - \\d{4}$";
    	public static final String REGEX_REGIDENT	= "\\d{6} \\- [1-4]\\d{6}";
    	public static final String REGEX_IP		= "([0-9]{1,3}) \\. ([0-9]{1,3}) \\. ([0-9]{1,3}) \\. ([0-9]{1,3})";
    
    	public static final String MSG_FORMAT_ID	= "아이디 형식을 확인하세요";
    	public static final String MSG_FORMAT_EMAIL	= "이메일 형식을 확인하세요";
    	public static final String MSG_FORMAT_PASSWORD	= "비밀번호 형식을 확인하세요";
    	public static final String MSG_FORMAT_ALL		= "입력값을 확인하세요";
    }
  • VSeminarUI.java

    package com.vseminar.vaadin_seminar2;
    
    import java.util.regex.Pattern;
    
    import javax.servlet.annotation.WebServlet;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import com.vaadin.annotations.Theme;
    import com.vaadin.annotations.VaadinServletConfiguration;
    import com.vaadin.annotations.Widgetset;
    import com.vaadin.data.validator.EmailValidator;
    import com.vaadin.data.validator.NullValidator;
    import com.vaadin.data.validator.StringLengthValidator;
    import com.vaadin.event.FieldEvents.BlurEvent;
    import com.vaadin.event.FieldEvents.BlurListener;
    import com.vaadin.server.FontAwesome;
    import com.vaadin.server.VaadinRequest;
    import com.vaadin.server.VaadinServlet;
    import com.vaadin.shared.ui.label.ContentMode;
    import com.vaadin.ui.Alignment;
    import com.vaadin.ui.Button;
    import com.vaadin.ui.HorizontalLayout;
    import com.vaadin.ui.Label;
    import com.vaadin.ui.Notification;
    import com.vaadin.ui.Notification.Type;
    import com.vaadin.ui.PasswordField;
    import com.vaadin.ui.TextField;
    import com.vaadin.ui.UI;
    import com.vaadin.ui.VerticalLayout;
    import com.vaadin.ui.themes.ValoTheme;
    import com.vseminar.vaadin_seminar2.util.Constants;
    import com.vseminar.vaadin_seminar2.util.PasswordValidator;
    
    /**
     * This UI is the application entry point. A UI may either represent a browser window
     * (or tab) or some part of a html page where a Vaadin application is embedded.
     * <p>
     * The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be
     * overridden to add component to the user interface and initialize non-component functionality.
     */
    /**
    *
    * Description
    *
    *
    * @author @user
    *
    * @version 1.0.0
    * @date 2018. 6. 16.
    *=======================================================================
    * Date            Name                     Revision History
    *=======================================================================
    * 2018. 6. 16.    hyperrookie@gmail.com    Creation
    *=======================================================================
    */
    @SuppressWarnings("serial")
    @Theme("vseminar")
    @Widgetset("com.vseminar.vaadin_seminar2.VSeminarWidgetset")
    public class VSeminarUI extends UI {
    
    	private static final Logger logger = LoggerFactory.getLogger(VSeminarUI.class);
    	private Label message;
    
        @Override
        protected void init(VaadinRequest vaadinRequest) {
    
        	logger.debug("vaadinRequest ----------> {}", vaadinRequest);
    
            final VerticalLayout layout = new VerticalLayout();
    
            final VerticalLayout loginPanel = new VerticalLayout();
            loginPanel.setSizeUndefined();
            loginPanel.setSpacing(true);
    
            Label top = new Label("예쁜누나들은 어서오세요~<br />너는 아니다....",ContentMode.HTML);
            top.addStyleName(ValoTheme.LABEL_H4);
            top.addStyleName(ValoTheme.LABEL_COLORED);
            loginPanel.addComponent(top);
    
            message = new Label();
            message.setVisible(false);
            message.setSizeFull();
            message.setStyleName(ValoTheme.LABEL_H4);
            message.setStyleName(ValoTheme.LABEL_FAILURE);
            loginPanel.addComponent(message);
    
            final HorizontalLayout loginFileds = new HorizontalLayout();
            loginFileds.setSizeUndefined();
            loginFileds.setSpacing(true);
    
            final TextField email = new TextField("Email");
            email.setIcon(FontAwesome.USER);
            email.addStyleName(ValoTheme.TEXTFIELD_INLINE_ICON);
            email.addBlurListener(e->{
    			if( email.getValue().length() > 0 && !Pattern.matches(Constants.REGEX_EMAIL, email.getValue())) {
    				Notification.show(Constants.MSG_FORMAT_EMAIL, Type.WARNING_MESSAGE);
    				email.focus();
    				logger.debug("{}", email.getValue());
    			}
            });
    
            final PasswordField password = new PasswordField("Password");
            password.setIcon(FontAwesome.LOCK);
            password.addStyleName(ValoTheme.TEXTFIELD_INLINE_ICON);
            password.addBlurListener(e->{
    			if (password.getValue().length() > 0 && !Pattern.matches(Constants.REGEX_PASSWORD, password.getValue())) {
    				Notification.show(Constants.MSG_FORMAT_PASSWORD, Type.WARNING_MESSAGE);
    				password.focus();
    			}
    		});
    
            final Button signon = new Button("signon");
            signon.setStyleName(ValoTheme.BUTTON_PRIMARY);
            signon.addClickListener(e->{
    
            	if ( !Pattern.matches(Constants.REGEX_EMAIL, email.getValue()) || !Pattern.matches(Constants.REGEX_PASSWORD, password.getValue()) ) {
            		logger.debug("{} / {}", email.getValue(),password.getValue());
            		message.setValue(Constants.MSG_FORMAT_ALL);
            		message.setVisible(true);
            		return;
            	}
            	message.setVisible(false);
            	logger.debug(UI.getCurrent().getPage().getWindowName());
            });
    
            loginFileds.addComponents(email,password,signon);
            loginFileds.setComponentAlignment(signon, Alignment.BOTTOM_LEFT);
    
            loginPanel.addComponent(loginFileds);
    
            layout.addComponent(loginPanel);
            layout.setMargin(true);
            layout.setSpacing(true);
            layout.setSizeFull();
            layout.setComponentAlignment(loginPanel, Alignment.MIDDLE_CENTER);
    
            setContent(layout);
        }
    
        @WebServlet(urlPatterns = "/*", name = "VSeminarUIServlet", asyncSupported = true)
        @VaadinServletConfiguration(ui = VSeminarUI.class, productionMode = false)
        public static class VSeminarUIServlet extends VaadinServlet {
        }
    }

 

반응형

'front-end & ui > vaadin legacy' 카테고리의 다른 글

vaadin session 실습  (0) 2018.06.17
vaadin layout 실습  (0) 2018.06.16
vaadin field validation 실습  (0) 2018.06.15
vaadin password validator  (0) 2018.06.15
vaadin 기초 정리  (0) 2018.06.14
Comments