YesYo.com MintState Forums
뒤로    YesYo.com MintState BBS > Tech > Javascript
검색
멤버이름    오토
비밀번호 
 

문자열 처리함수

페이지 정보

작성자 MintState 댓글 0건 조회 11,440회 작성일 08-11-10 12:05

본문

문자열 처리함수

//******************************************************************************************//
//* 입력(타이핑)시 문자열 체크함수 
//* 
//* 사용방법
//*     <input type="text" id="ObjectIDName" 객체이벤트="return input.isNum();" />
//*
//* 설명
//*     input.isFunc() : 입력이 해당펑션키일때 true 반환.
//*     input.isNum()  : 입력이 숫자(키보드또는 키패드)일때 true 반환.
//*     input.isEng()  : 입력이 영문자일때 true 반환.
//*     input.isWord() : 입력이 숫자또는 영문자일때 true 반환.
//------------------------------------------------------------------------------------------//
var input = {
    // 펑션
    isFunc : function(evt) {
        switch(evt.keyCode) {
            case 8 : //back  
            case 35: //end   
            case 36: //home  
            case 37: //left
            case 38: //top
            case 39: //right
            case 40: //bottom
            case 45: //insert
            case 46: //delete
            case 9 : //tab
            //case 188: //comma
            return true;
        }
        return false;
    },
    // 숫자
    isNum : function(evt) {
        if(!evt.shiftKey && evt.keyCode >= 48 && evt.keyCode <= 57)  { return true; } //keyboard's num
        else if (!evt.shiftKey && evt.keyCode >= 96 && evt.keyCode <= 105) { return true; } //pad's num
        else { return this.isFunc(evt); }
    },
    // 영문자
    isEng : function(evt) {
        if(evt.keyCode >= 65 && evt.keyCode <= 90) { return true; } //keyboard's char
        else { return this.isFunc(evt); }
    },
    // 영문자 + 숫자
    isWord : function(evt) {
        if(evt.keyCode >= 65 && evt.keyCode <= 90) { return true; } //keyboard's char
        else { return this.isNum(evt); }
    }
}


//******************************************************************************************//
//* 문자열 처리 함수 //* 
//* 사용방법
//*     var TempString = "abcd1234";
//*     alert(TempString.isValues());
//*
//* 설명
//*      1. String.isValues() : 문자가 하나라로 있을시 true 반환.
//*      2. String.isValidFormat(format) : Argument에 따른 정규표현식 처리.
//*      3. String.isWord() : 영문자와 숫자 조합일때 true 반환.
//*      4. String.isNum() : 숫자 조합일때만 true 반환.
//*      5. String.isEng() : 영문자일때만 true 반환.
//*      6. String.isParticular() : 특수문자가 문자열내에 있을경우 true 반환.
//*      7. String.isKor() : 한글일때만 true 반환.
//*      8. String.isPhone() : "2,3자리-3,4자리-4자리"의 숫자조합일 경우에만 true 반환.
//*      9. String.isEmail() : 이메일 형식일 경우에만 true 반환.
//*     10. String.isSpace() : 공백이 하나라도 있을경우 true 반환.
//*     11. String.trim() : 공백 모두제거
//*     12. String.replaceAll(replace, string) : replace문자를 string문자로 모두 변환
//*     13. String.isIDPWD() : 4~12자리의 영문자+숫자 조합일 경우에만 true 반환.
//*     14. String.isSSN() : 현재 주민등록번호 알고리즘에 맞을경우만 true 반환. [-없이 13자리값 입력]
//*     15. String.isPost() : 우편번호. [3자리숫자 - 3자리숫자]
//*     16. String.isLen(len) : 문자열이 비교길이 보다 클경우 True, 작거나 같을경우 False반환
//*     17. String.Length() : 문자열의 길이(Byte) 반환
//*     18. String.Slice(len) : 입력된 길이만큼 문자열을 잘라서 반환.
//------------------------------------------------------------------------------------------//
//* 정규표현식 패턴.
var Pattern = {
      PARTICULAR : /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\='\"\|]/ 
    , ENGLISH : /^([a-zA-Z]+)$/ /* /^(\w[^0-9_]+)$/ */
    , NUMBER : /^(\d+)$/ 
    , ENGNUM : /^(\w+)$/ 
    , SPACE : /(^\s*)|(\s*$)/gi 
    , EMAIL : /^((\w|[\-\.])+)@((\w|[\-\.])+)\.([A-Za-z]+)$/ 
    , PHONE : /^(0(\d{1,2}))-(\d{3,4})-(\d{4})$/ 
    , IDPWD : /^(\w{4,12})$/
    , SSN : /^(\d{13})$/
    , POST : /^((\d{3}))-(\d{3})$/ 
}
//* 패턴에 따른 값 유무 체크
String.prototype.isValues = function() {
return this.length > 0 && this != null;
}
//* 정규표현식 검사
String.prototype.isValidFormat = function(format) {
    return this.search(format) != -1
}
//* 영문자 + 숫자
String.prototype.isWord = function() {
    return this.isValidFormat(Pattern.ENGNUM);
}
//* 숫자
String.prototype.isNum = function() {
    return this.isValidFormat(Pattern.NUMBER);
}
//* 영문자
String.prototype.isEng = function() {
    return this.isValidFormat(Pattern.ENGLISH);
}
//* 특수문자 여부
String.prototype.isParticular = function() {
    return this.isValidFormat(Pattern.PARTICULAR);
}
//* 한글
String.prototype.isKor = function() {
    for (var i=0; i<this.length; i++) {
        chrCode = this.charCodeAt(i);
        if (chrCode > 128) { return true; break; }
    }
    return false;
}
//* 전화번호
String.prototype.isPhone = function() {
    return this.isValidFormat(Pattern.PHONE);
}
//* 이메일
String.prototype.isEmail = function() {
    return this.isValidFormat(Pattern.EMAIL);
}
//* 공백
String.prototype.isSpace = function() {
    return this.isValidFormat(Pattern.SPACE);
}
//* 공백제거
String.prototype.trim = function() {
    return this.replace(Pattern.SPACE, "");
}
//* 문자 전체 치환
String.prototype.replaceAll = function(replace, string) {
    var tmpStr = this.trim();
    if (tmpStr != "" && replace != string) 
        while ( tmpStr.indexOf(replace) > -1 ) { tmpStr = tmpStr.replace(replace, string); }
    return tmpStr;
}
//* 아이디/비밀번호
String.prototype.isIDPWD = function() {
     return this.isValidFormat(Pattern.IDPWD);
}
//* 주민등록번호
String.prototype.isSSN = function() {
     if (this.isValidFormat(Pattern.SSN)) {
        var chk = 0;
        for (var i=0; i<6; i++)  { chk += ( (i+2) * parseInt( this.charAt(i) )); }
        for (var i=6; i<12; i++) { chk += ( (i%8+2) * parseInt( this.charAt(i) )); }
        chk = (11 - (chk % 11)) % 10;
        if ( chk = parseInt(this.charAt(12)) ) return true;
        return false;
     }
     return false;
}
//* 우편번호
String.prototype.isPost = function() {
     return this.isValidFormat(Pattern.POST);
}
//* 문자열 길이 검사(Byte단위)
String.prototype.isLen = function(len) {
    return rtn = (len <= this.Length()) ? true : false ;
}
//* 문자열 길이 체크(Byte단위)
String.prototype.Length = function() {
    var bytLen = 0, strSlice = "";
    for (var i=0; i<this.length; i++) {
        strSlice = this.substring(i, i+1);
        bytLen += Math.sqrt(Math.abs(escape(strSlice).length - 2));
    }
    return bytLen;
}
//* 문자열 길이만큼 자르기.
String.prototype.Slice = function(len) {
    var bytLen = 0, strSlice = "", strRtn = "";
    for (var i=0; i<this.length; i++) {
        strSlice = this.substring(i, i+1);
        bytLen += Math.sqrt(Math.abs(escape(strSlice).length - 2));
        strRtn += strSlice;
        if (bytLen >= len) { return strRtn; break; }
    }
    return strRtn;
}

//******************************************************************************************//

출처 : http://ulgom.net/

댓글목록

등록된 댓글이 없습니다.

Total 178건 6 페이지
Javascript 목록
번호 제목 글쓴이 조회 날짜
78 MintState 9631 11-17
77 MintState 14397 11-17
76 MintState 12312 11-17
75 MintState 13861 11-17
74 MintState 20175 11-17
73 MintState 14263 11-17
72 MintState 12832 11-17
71 MintState 13830 11-17
70 MintState 11808 11-17
69 MintState 10567 11-17
68 MintState 9390 11-17
67 MintState 10167 11-17
66 MintState 9854 11-17
65 MintState 11085 11-17
64 MintState 9946 11-17
63 MintState 14777 11-11
열람중 MintState 11441 11-10
61 MintState 10047 11-10
60 MintState 10835 11-10
59 MintState 12007 11-10
게시물 검색
모바일 버전으로 보기
CopyRight ©2004 - 2024, YesYo.com MintState. ™