ㅤㅤㅤ

날짜, 숫자, 영어 등 유효성 체크 검사코드 본문

プログラミング/JAVA

날짜, 숫자, 영어 등 유효성 체크 검사코드

ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ 2017. 6. 20. 09:45


"(?:(?:19|20)[\\d]{2})-[0-1][0-2]-(0[1-9]|[12][0-9]|3[01])$"


위에는 1990-01-01 << 이것만 오케이!


아래 정규식은 1900-01-01 ~ 2099-12-31 년까지 체크할 수 있다.

1990-01-01 ok

19900101 ok

08-01 ok

0801 ok

 

1
2
3
4
5
6
7
8
9
10
  String anni_date = "1999-01-01";
  Pattern pattern =  Pattern.compile("^((19|20)\\d\\d)?([- /.])?(0[1-9]|1[012])([- /.])?(0[1-9]|[12][0-9]|3[01])$");
  Matcher matcher = pattern.matcher(anni_date);
 
 
  if(matcher.find()==false// 체크
  {
      Alert(this"기념일은 YYYY-MM-DD형식으로 입력해 주세요.");
  } 






한글 구분

한글은 "가" ~ "힣" 까지 차례대로 유니코드 정수로 표현합니다.

입력받은문자(ch)가 다음 조건에 만족하면 한글입니다.

if (ch>='가' && ch<='힣')

 

 

정규표현식

^[0-9]*$    : 숫자
^[z-zA-Z]*$ : 영문자
^[가-힣]*$  : 한글
^[a-zA-Z0-9]: 영어/숫자 

 

boolean a = Pattern.matches("[a-z]", "a");
boolean b = Pattern.matches("[A-Z]", "a");
boolean c = Pattern.matches("[0-9]", "0");
boolean d = Pattern.matches("[가-힝]", "락");

 

Package java.util.regex

Classes for matching character sequences against patterns specified by regular expressions.

 

Method matches

public static boolean matches(String regex,
                              CharSequence input) 

A typical invocation sequence is thus

boolean b = Pattern.matches("a*b", "aaaaab")

참고

자바식 정규표현 방법 

http://blog.daum.net/question0921/419 







    Comments