티스토리 뷰

Java Script & HTML

정규표현식 모음

LichKing 2014. 12. 24. 10:22

/--------------------------------------------------------------------------------/
사용법
파일 첨부후
해당 input 개체에
onKeyDown="nr_phone(this);" onKeyPress="nr_phone(this);" onKeyUp="nr_phone(this);"
위 이벤트를 등록.

<!-- 노멀라이즈 함수, 숫자,영어,주민번,사업자번호 체크.-->
<script language='JavaScript' src='nr_func.js'></script>

<input type=text name='name' size=20 maxlength=20  onKeyDown="nr_phone(this);" onKeyPress="nr_phone(this);" onKeyUp="nr_phone(this);">

/-------------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------*/
/*
한글의 경우 키 입력시 바로바로 작업이 안되기 때문에
onchange 와 onblur 등 이벤트도 같이 등록.
*/

// 한글만 입력받기 (초성체 무시)
// 나머지 글자 무시
function nr_han(this_s,type){
    /*
    type
    -> 'c' : 초성 포함
    -> 's' : 초성 포함 + 공백 포함
    -> '' : 초성, 공백 무시
    */
    temp_value = this_s.value.toString();
    regexp = '';
    repexp = '';
    switch(type){
        case 'c': regexp = /[^ㄱ-ㅎ가-힣]/g;break;
        case 's': regexp = /[^ㄱ-ㅎ가-힣\s]/g;break;
        case '':    regexp = /[^가-힣]/g; break;
        default : regexp = /[^ㄱ-ㅎ가-힣\s]/g;
    }
    if(regexp.test(temp_value))
    {
        temp_value = temp_value.replace(regexp,repexp);
        this_s.value = temp_value;
    }
}

/*-------------------------------------------------------------------------------*/

// 한글만 입력받기 (초성체 포함)
// 나머지 글자 무시
function nr_han_cho(this_s){
    nr_han(this_s,'c');
}

/*-------------------------------------------------------------------------------*/

// 한글만 입력받기 (초성체 포함, 공백 포함)
// 나머지 글자 무시
function nr_han_cho_space(this_s){
    nr_han(this_s,'s');
}


/*-------------------------------------------------------------------------------*/
function nr_numeng(this_s){
    temp_value = this_s.value.toString();
    regexp = /[^0-0a-zA-Z]/g;
    repexp = '';
    temp_value = temp_value.replace(regexp,repexp);
    this_s.value = temp_value;
}

/*-------------------------------------------------------------------------------*/
// 나머지 글자 무시
function nr_num(this_s,type){
    /*
    type
    -> 'int' : 양의 정수
    -> 'float' : 양의 실수
    -> '-int' : 음의 정수 포함
    -> '-int' : 음의 실수 포함
    */
    temp_value = this_s.value.toString();
    regexp = /[^-\.0-9]/g;
    repexp = '';
    temp_value = temp_value.replace(regexp,repexp);
    regexp = '';
    repexp = '';
    switch(type){
        case 'int':     regexp = /[^0-9]/g; break;
        case 'float':regexp = /^(-?)([0-9]*)(\.?)([^0-9]*)([0-9]*)([^0-9]*)/; break;
        case '-int':    regexp = /^(-?)([0-9]*)([^0-9]*)([0-9]*)([^0-9]*)/;break;
        case '-float':regexp = /^(-?)([0-9]*)(\.?)([^0-9]*)([0-9]*)([^0-9]*)/; break;
        default : regexp = /[^0-9]/g; break;
    }
    switch(type){
        case 'int':repexp = '';break;
        case 'float':repexp = '$2$3$5';break;
        case '-int':    repexp = '$1$2$4';break;
        case '-float':repexp = '$1$2$3$5'; break;
        default : regexp = /[^0-9]/g; break;
    }
    temp_value = temp_value.replace(regexp,repexp);
    this_s.value = temp_value;
}
// 양의 정수만 입력받기
function nr_num_int(this_s){
    nr_num(this_s,'int');
}
// 양의 실수만 입력받기
function nr_num_float(this_s){
    nr_num(this_s,'float');
}

/*-------------------------------------------------------------------------------*/

// 영어만 입력받기  (대소문자)
// 나머지 글자 무시
function nr_eng(this_s,type){
    temp_value = this_s.value.toString();
    regexp = '';
    repexp = '';
    switch(type){
        case 'small':regexp = /[^a-z]/g;break;
        case 'big':regexp = /[^A-Z]/g;break;
        case 'all':regexp = /[^a-z]/i;break;
        default :regexp = /[^a-z]/i;break;
    }
    temp_value = temp_value.replace(regexp,repexp);
    this_s.value = temp_value;
}

// 영어만 입력받기  (소문자)
// 나머지 글자 무시
function nr_eng_small(this_s){
    nr_eng(this_s,'small');
}

// 영어만 입력받기  (대문자)
// 나머지 글자 무시
function nr_eng_big(this_s){
    nr_eng(this_s,'big');
}
// 전화번호 규격에 맞게 DDD-MM~M-XXXX
// 나머지 글자 무시
function nr_phone(this_s)
{
    temp_value = this_s.value.toString();
    temp_value = temp_value.replace(/[^0-9]/g,'');
    temp_value = temp_value.replace(/(0(?:2|[0-9]{2}))([0-9]+)([0-9]{4}$)/,"$1-$2-$3");
    this_s.value = temp_value;
}

/*-------------------------------------------------------------------------------*/


// 주민등록 번호 규격에 맞게 123456-1234567  //검증하지 않음.
// 나머지 글자 무시
function nr_jumin(this_s)
{
    temp_value = this_s.value.toString();
    temp_value = temp_value.replace(/[^0-9]/g,'');
    temp_value = temp_value.substr(0,13);
    temp_value = temp_value.replace(/([0-9]{6})([0-9]{7}$)/,"$1-$2");
    this_s.value = temp_value;
}



/*-------------------------------------------------------------------------------*/

// 사업자 등록 번호 규격에 맞게 123-12-12345  //검증하지 않음.
// 나머지 글자 무시
function nr_company_num(this_s)
{
    temp_value = this_s.value.toString();
    temp_value = temp_value.replace(/[^0-9]/g,'');
    temp_value = temp_value.substr(0,10);
    temp_value = temp_value.replace(/([0-9]{3})([0-9]{2})([0-9]{5}$)/,"$1-$2-$3");
    this_s.value = temp_value;
}

//----------------------------------------------=--------------------------------------
그냥 전에 만들었고, 가져왔던 것을 모음집으로 만든겁니다.
숫자의 경우 음수도 체크하도록 했습니다.
뭐, 따지면 더 간단한 방법도 있을겁니다.

#사용법은?

<!-- 노멀라이즈 함수, 숫자,영어,주민번,사업자번호 체크.-->
<script language='JavaScript' src='nr_func.js'></script>

<input type=text name='name' size=20 maxlength=20  onKeyDown="nr_phone(this);" onKeyPress="nr_phone(this);" onKeyUp="nr_phone(this);">

이처럼 .js로 만들든 어쨌든
HTML에 집어 넣고

onKeyDown="nr_phone(this);" onKeyPress="nr_phone(this);" onKeyUp="nr_phone(this);"
이렇게 이벤트를 등록하면 됩니다.

#주의점은?

한글 체크의 경우 한글을 적을 때 바로바로 onKeyXXXX()이벤트가 일어나지 않습니다.
onblur()와 onChange()도 같이 등록해주세요.

#동작은?

해당 함수에서 값을 체크해서 정해진 값이 아니면
전부 무시해버립니다.
즉, 양의 정수만 입력이 가능한
nr_num_int()는 숫자만 입력이 가능합니다. 나머지 글자는 무시!

 

출처 : http://blog.naver.com/falconer00/90004531500


오래전글이라 그런지 그대로 복사해서 쓰기엔 약간 무리가 있는게 몇몇 있고 알맞게 잘쓰면 요긴한것같음.

'Java Script & HTML' 카테고리의 다른 글

javascript AJAX  (0) 2015.01.15
jquery checkbox 제어  (0) 2015.01.02
null과 undefined 차이  (0) 2014.12.18
radio button 체크된 값 가져오기  (0) 2014.12.12
TEXTAREA 줄바꿈 저장  (2) 2014.12.12
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함