쓸모있는 정규식 모음 JS버전

/*-------------------------------------------------------------------------------/
만든이 : mins
mins01(at)lycos.co.kr
http://www.mins01.com

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

<!-- 노멀라이즈 함수, 숫자,영어,주민번,사업자번호 체크.-->
<script language='JavaScript' src='<?=$board_skin?>/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);">

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

/*-------------------------------------------------------------------------------*/
/*
한글의 경우 초성을 포함하지 않으면
onKey~~~ 로 인해서 한글을 적을 수 없습니다.
onKey~~~ 이벤트는 빼주시고
onchange 와 onblur 등 이벤트에 등록해주세요.
*/

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




IP 검증 정규식
https://www.w3resource.com/javascript/form/ip-address-validation.php
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/


댓글
  • No Nickname
    No Comment
  • 권한이 없습니다.
    {{m_row.m_nick}}
    -
목록형 📷 갤러리형
제목
[기본형] HTML (with 부트스트랩5.3 , jquery 3.7, vue.js)
유용한 리눅스(LINUX) 명령어
[공지] 기술 게시판
4.28
4.29
4.30
5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9
5.10
5.11
5.12
5.13
5.14
5.15
5.16
5.17
5.18
5.19
5.20
5.21
5.22
5.23
5.24
5.25
5.26
5.27
5.28
5.29
5.30
5.31
6.1