javascript
String Function모음 String을 자바처럼 핸들링해보잣~!
jeeyong
2007. 11. 19. 16:29
String Prototype Function reference.
1 /*--------------------------------------------------------------------------------*\
2 * JavaScript framework, version 2.0
3 *
4 * Date : 2006. 08. 15.
5 * Copyright 2004-2007 by Vricks Studio. All right reserved.
6 * @author Jeff Yang routine@vricks.com
7 * 자주 쓰이는 스트링 관련 prototype관련 정리
8 \*--------------------------------------------------------------------------------*/
9
10 /*---------------------------------------------------------------------------------*\
11 * String prototype
12 \*---------------------------------------------------------------------------------*/
13 //-----------------------------------------------------------------------------
14 // 문자의 좌, 우 공백 제거
15 // @return : String
16 //-----------------------------------------------------------------------------
17 String.prototype.trim = function() {
18 return this.replace(/(^\s*)|(\s*$)/g, "");
19 }
20 //-----------------------------------------------------------------------------
21 // 문자의 좌 공백 제거
22 // @return : String
23 //-----------------------------------------------------------------------------
24 String.prototype.ltrim = function() {
25 return this.replace(/(^\s*)/, "");
26 }
27 //-----------------------------------------------------------------------------
28 // 문자의 우 공백 제거
29 // @return : String
30 //-----------------------------------------------------------------------------
31 String.prototype.rtrim = function() {
32 return this.replace(/(\s*$)/, "");
33 }
34 //-----------------------------------------------------------------------------
35 // 문자열의 byte 길이 반환
36 // @return : int
37 //-----------------------------------------------------------------------------
38 String.prototype.byte = function() {
39 var cnt = 0;
40 for (var i = 0; i < this.length; i++) {
41 if (this.charCodeAt(i) > 127)
42 cnt += 2;
43 else
44 cnt++;
45 }
46 return cnt;
47 }
48 //-----------------------------------------------------------------------------
49 // 정수형으로 변환
50 // @return : String
51 //-----------------------------------------------------------------------------
52 String.prototype.int = function() {
53 if(!isNaN(this)) {
54 return parseInt(this);
55 }
56 else {
57 return null;
58 }
59 }
60 //-----------------------------------------------------------------------------
61 // 숫자만 가져 오기
62 // @return : String
63 //-----------------------------------------------------------------------------
64 String.prototype.num = function() {
65 return (this.trim().replace(/[^0-9]/g, ""));
66 }
67 //-----------------------------------------------------------------------------
68 // 숫자에 3자리마다 , 를 찍어서 반환
69 // @return : String
70 //-----------------------------------------------------------------------------
71 String.prototype.money = function() {
72 var num = this.trim();
73 while((/(-?[0-9]+)([0-9]{3})/).test(num)) {
74 num = num.replace((/(-?[0-9]+)([0-9]{3})/), "$1,$2");
75 }
76 return num;
77 }
78 //-----------------------------------------------------------------------------
79 // 숫자의 자리수(cnt)에 맞도록 반환
80 // @return : String
81 //-----------------------------------------------------------------------------
82 String.prototype.digits = function(cnt) {
83 var digit = "";
84 if (this.length < cnt) {
85 for(var i = 0; i < cnt - this.length; i++) {
86 digit += "0";
87 }
88 }
89 return digit + this;
90 }
91 //-----------------------------------------------------------------------------
92 // " -> " ' -> '로 바꾸어서 반환
93 // @return : String
94 //-----------------------------------------------------------------------------
95 String.prototype.quota = function() {
96 return this.replace(/"/g, """).replace(/'/g, "'");
97 }
98 //-----------------------------------------------------------------------------
99 // 파일 확장자만 가져오기
100 // @return : String
101 //-----------------------------------------------------------------------------
102 String.prototype.ext = function() {
103 return (this.indexOf(".") < 0) ? "" : this.substring(this.lastIndexOf(".") + 1, this.length);
104 }
105 //-----------------------------------------------------------------------------
106 // URL에서 파라메터 제거한 순수한 url 얻기
107 // @return : String
108 //-----------------------------------------------------------------------------
109 String.prototype.uri = function() {
110 var arr = this.split("?");
111 arr = arr[0].split("#");
112 return arr[0];
113 }
114
115 /*---------------------------------------------------------------------------------*\
116 * 각종 체크 함수들
117 \*---------------------------------------------------------------------------------*/
118 //-----------------------------------------------------------------------------
119 // 정규식에 쓰이는 특수문자를 찾아서 이스케이프 한다.
120 // @return : String
121 //-----------------------------------------------------------------------------
122 String.prototype.meta = function() {
123 var str = this;
124 var result = ""
125 for(var i = 0; i < str.length; i++) {
126 if((/([\$\(\)\*\+\.\[\]\?\\\^\{\}\|]{1})/).test(str.charAt(i))) {
127 result += str.charAt(i).replace((/([\$\(\)\*\+\.\[\]\?\\\^\{\}\|]{1})/), \\$1);
128 }
129 else {
130 result += str.charAt(i);
131 }
132 }
133 return result;
134 }
135 //-----------------------------------------------------------------------------
136 // 정규식에 쓰이는 특수문자를 찾아서 이스케이프 한다.
137 // @return : String
138 //-----------------------------------------------------------------------------
139 String.prototype.remove = function(pattern) {
140 return (pattern == null) ? this : eval("this.replace(/[" + pattern.meta() + "]/g, \"\")");
141 }
142 //-----------------------------------------------------------------------------
143 // 최소 최대 길이인지 검증
144 // str.isLength(min [,max])
145 // @return : boolean
146 //-----------------------------------------------------------------------------
147 String.prototype.isLength = function() {
148 var min = arguments[0];
149 var max = arguments[1] ? arguments[1] : null;
150 var success = true;
151 if(this.length < min) {
152 success = false;
153 }
154 if(max && this.length > max) {
155 success = false;
156 }
157 return success;
158 }
159 //-----------------------------------------------------------------------------
160 // 최소 최대 바이트인지 검증
161 // str.isByteLength(min [,max])
162 // @return : boolean
163 //-----------------------------------------------------------------------------
164 String.prototype.isByteLength = function() {
165 var min = arguments[0];
166 var max = arguments[1] ? arguments[1] : null;
167 var success = true;
168 if(this.byte() < min) {
169 success = false;
170 }
171 if(max && this.byte() > max) {
172 success = false;
173 }
174 return success;
175 }
176 //-----------------------------------------------------------------------------
177 // 공백이나 널인지 확인
178 // @return : boolean
179 //-----------------------------------------------------------------------------
180 String.prototype.isBlank = function() {
181 var str = this.trim();
182 for(var i = 0; i < str.length; i++) {
183 if ((str.charAt(i) != "\t") && (str.charAt(i) != "\n") && (str.charAt(i)!="\r")) {
184 return false;
185 }
186 }
187 return true;
188 }
189 //-----------------------------------------------------------------------------
190 // 숫자로 구성되어 있는지 학인
191 // arguments[0] : 허용할 문자셋
192 // @return : boolean
193 //-----------------------------------------------------------------------------
194 String.prototype.isNum = function() {
195 return (/^[0-9]+$/).test(this.remove(arguments[0])) ? true : false;
196 }
197 //-----------------------------------------------------------------------------
198 // 영어만 허용 - arguments[0] : 추가 허용할 문자들
199 // @return : boolean
200 //-----------------------------------------------------------------------------
201 String.prototype.isEng = function() {
202 return (/^[a-zA-Z]+$/).test(this.remove(arguments[0])) ? true : false;
203 }
204 //-----------------------------------------------------------------------------
205 // 숫자와 영어만 허용 - arguments[0] : 추가 허용할 문자들
206 // @return : boolean
207 //-----------------------------------------------------------------------------
208 String.prototype.isEngNum = function() {
209 return (/^[0-9a-zA-Z]+$/).test(this.remove(arguments[0])) ? true : false;
210 }
211 //-----------------------------------------------------------------------------
212 // 숫자와 영어만 허용 - arguments[0] : 추가 허용할 문자들
213 // @return : boolean
214 //-----------------------------------------------------------------------------
215 String.prototype.isNumEng = function() {
216 return this.isEngNum(arguments[0]);
217 }
218 //-----------------------------------------------------------------------------
219 // 아이디 체크 영어와 숫자만 체크 첫글자는 영어로 시작 - arguments[0] : 추가 허용할 문자들
220 // @return : boolean
221 //-----------------------------------------------------------------------------
222 String.prototype.isUserid = function() {
223 return (/^[a-zA-z]{1}[0-9a-zA-Z]+$/).test(this.remove(arguments[0])) ? true : false;
224 }
225 //-----------------------------------------------------------------------------
226 // 한글 체크 - arguments[0] : 추가 허용할 문자들
227 // @return : boolean
228 //-----------------------------------------------------------------------------
229 String.prototype.isKor = function() {
230 return (/^[가-힣]+$/).test(this.remove(arguments[0])) ? true : false;
231 }
232 //-----------------------------------------------------------------------------
233 // 주민번호 체크 - arguments[0] : 주민번호 구분자
234 // XXXXXX-XXXXXXX
235 // @return : boolean
236 //-----------------------------------------------------------------------------
237 String.prototype.isJumin = function() {
238 var arg = arguments[0] ? arguments[0] : "";
239 var jumin = eval("this.match(/[0-9]{2}[01]{1}[0-9]{1}[0123]{1}[0-9]{1}" + arg + "[1234]{1}[0-9]{6}$/)");
240 if(jumin == null) {
241 return false;
242 }
243 else {
244 jumin = jumin.toString().num().toString();
245 }
246 // 생년월일 체크
247 var birthYY = (parseInt(jumin.charAt(6)) == (1 ||2)) ? "19" : "20";
248 birthYY += jumin.substr(0, 2);
249 var birthMM = jumin.substr(2, 2) - 1;
250 var birthDD = jumin.substr(4, 2);
251 var birthDay = new Date(birthYY, birthMM, birthDD);
252 if(birthDay.getYear() % 100 != jumin.substr(0,2) || birthDay.getMonth() != birthMM || birthDay.getDate() != birthDD) {
253 return false;
254 }
255 var sum = 0;
256 var num = [2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5]
257 var last = parseInt(jumin.charAt(12));
258 for(var i = 0; i < 12; i++) {
259 sum += parseInt(jumin.charAt(i)) * num[i];
260 }
261 return ((11 - sum % 11) % 10 == last) ? true : false;
262 }
263 //-----------------------------------------------------------------------------
264 // 외국인 등록번호 체크 - arguments[0] : 등록번호 구분자
265 // XXXXXX-XXXXXXX
266 // @return : boolean
267 //-----------------------------------------------------------------------------
268 String.prototype.isForeign = function() {
269 var arg = arguments[0] ? arguments[0] : "";
270 var jumin = eval("this.match(/[0-9]{2}[01]{1}[0-9]{1}[0123]{1}[0-9]{1}" + arg + "[5678]{1}[0-9]{1}[02468]{1}[0-9]{2}[6789]{1}[0-9]{1}$/)");
271 if(jumin == null) {
272 return false;
273 }
274 else {
275 jumin = jumin.toString().num().toString();
276 }
277 // 생년월일 체크
278 var birthYY = (parseInt(jumin.charAt(6)) == (5 || 6)) ? "19" : "20";
279 birthYY += jumin.substr(0, 2);
280 var birthMM = jumin.substr(2, 2) - 1;
281 var birthDD = jumin.substr(4, 2);
282 var birthDay = new Date(birthYY, birthMM, birthDD);
283 if(birthDay.getYear() % 100 != jumin.substr(0,2) || birthDay.getMonth() != birthMM || birthDay.getDate() != birthDD) {
284 return false;
285 }
286 if((parseInt(jumin.charAt(7)) * 10 + parseInt(jumin.charAt(8))) % 2 != 0) {
287 return false;
288 }
289 var sum = 0;
290 var num = [2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5]
291 var last = parseInt(jumin.charAt(12));
292 for(var i = 0; i < 12; i++) {
293 sum += parseInt(jumin.charAt(i)) * num[i];
294 }
295 return (((11 - sum % 11) % 10) + 2 == last) ? true : false;
296 }
297 //-----------------------------------------------------------------------------
298 // 사업자번호 체크 - arguments[0] : 등록번호 구분자
299 // XX-XXX-XXXXX
300 // @return : boolean
301 //-----------------------------------------------------------------------------
302 String.prototype.isBiznum = function() {
303 var arg = arguments[0] ? arguments[0] : "";
304 var biznum = eval("this.match(/[0-9]{3}" + arg + "[0-9]{2}" + arg + "[0-9]{5}$/)");
305 if(biznum == null) {
306 return false;
307 }
308 else {
309 biznum = biznum.toString().num().toString();
310 }
311 var sum = parseInt(biznum.charAt(0));
312 var num = [0, 3, 7, 1, 3, 7, 1, 3];
313 for(var i = 1; i < 8; i++) sum += (parseInt(biznum.charAt(i)) * num[i]) % 10;
314 sum += Math.floor(parseInt(parseInt(biznum.charAt(8))) * 5 / 10);
315 sum += (parseInt(biznum.charAt(8)) * 5) % 10 + parseInt(biznum.charAt(9));
316 return (sum % 10 == 0) ? true : false;
317 }
318 //-----------------------------------------------------------------------------
319 // 법인 등록번호 체크 - arguments[0] : 등록번호 구분자
320 // XXXXXX-XXXXXXX
321 // @return : boolean
322 //-----------------------------------------------------------------------------
323 String.prototype.isCorpnum = function() {
324 var arg = arguments[0] ? arguments[0] : "";
325 var corpnum = eval("this.match(/[0-9]{6}" + arg + "[0-9]{7}$/)");
326 if(corpnum == null) {
327 return false;
328 }
329 else {
330 corpnum = corpnum.toString().num().toString();
331 }
332 var sum = 0;
333 var num = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
334 var last = parseInt(corpnum.charAt(12));
335 for(var i = 0; i < 12; i++) {
336 sum += parseInt(corpnum.charAt(i)) * num[i];
337 }
338 return ((10 - sum % 10) % 10 == last) ? true : false;
339 }
340 //-----------------------------------------------------------------------------
341 // 이메일의 유효성을 체크
342 // @return : boolean
343 //-----------------------------------------------------------------------------
344 String.prototype.isEmail = function() {
345 return (/\w+([-+.]\w+)*@\w+([-.]\w+)*\.[a-zA-Z]{2,4}$/).test(this.trim());
346 }
347 //-----------------------------------------------------------------------------
348 // 전화번호 체크 - arguments[0] : 전화번호 구분자
349 // @return : boolean
350 //-----------------------------------------------------------------------------
351 String.prototype.isPhone = function() {
352 var arg = arguments[0] ? arguments[0] : "";
353 return eval("(/(02|0[3-9]{1}[0-9]{1})" + arg + "[1-9]{1}[0-9]{2,3}" + arg + "[0-9]{4}$/).test(this)");
354 }
355 //-----------------------------------------------------------------------------
356 // 핸드폰번호 체크 - arguments[0] : 핸드폰 구분자
357 // @return : boolean
358 //-----------------------------------------------------------------------------
359 String.prototype.isMobile = function() {
360 var arg = arguments[0] ? arguments[0] : "";
361 return eval("(/01[016789]" + arg + "[1-9]{1}[0-9]{2,3}" + arg + "[0-9]{4}$/).test(this)");
362 }
2 * JavaScript framework, version 2.0
3 *
4 * Date : 2006. 08. 15.
5 * Copyright 2004-2007 by Vricks Studio. All right reserved.
6 * @author Jeff Yang routine@vricks.com
7 * 자주 쓰이는 스트링 관련 prototype관련 정리
8 \*--------------------------------------------------------------------------------*/
9
10 /*---------------------------------------------------------------------------------*\
11 * String prototype
12 \*---------------------------------------------------------------------------------*/
13 //-----------------------------------------------------------------------------
14 // 문자의 좌, 우 공백 제거
15 // @return : String
16 //-----------------------------------------------------------------------------
17 String.prototype.trim = function() {
18 return this.replace(/(^\s*)|(\s*$)/g, "");
19 }
20 //-----------------------------------------------------------------------------
21 // 문자의 좌 공백 제거
22 // @return : String
23 //-----------------------------------------------------------------------------
24 String.prototype.ltrim = function() {
25 return this.replace(/(^\s*)/, "");
26 }
27 //-----------------------------------------------------------------------------
28 // 문자의 우 공백 제거
29 // @return : String
30 //-----------------------------------------------------------------------------
31 String.prototype.rtrim = function() {
32 return this.replace(/(\s*$)/, "");
33 }
34 //-----------------------------------------------------------------------------
35 // 문자열의 byte 길이 반환
36 // @return : int
37 //-----------------------------------------------------------------------------
38 String.prototype.byte = function() {
39 var cnt = 0;
40 for (var i = 0; i < this.length; i++) {
41 if (this.charCodeAt(i) > 127)
42 cnt += 2;
43 else
44 cnt++;
45 }
46 return cnt;
47 }
48 //-----------------------------------------------------------------------------
49 // 정수형으로 변환
50 // @return : String
51 //-----------------------------------------------------------------------------
52 String.prototype.int = function() {
53 if(!isNaN(this)) {
54 return parseInt(this);
55 }
56 else {
57 return null;
58 }
59 }
60 //-----------------------------------------------------------------------------
61 // 숫자만 가져 오기
62 // @return : String
63 //-----------------------------------------------------------------------------
64 String.prototype.num = function() {
65 return (this.trim().replace(/[^0-9]/g, ""));
66 }
67 //-----------------------------------------------------------------------------
68 // 숫자에 3자리마다 , 를 찍어서 반환
69 // @return : String
70 //-----------------------------------------------------------------------------
71 String.prototype.money = function() {
72 var num = this.trim();
73 while((/(-?[0-9]+)([0-9]{3})/).test(num)) {
74 num = num.replace((/(-?[0-9]+)([0-9]{3})/), "$1,$2");
75 }
76 return num;
77 }
78 //-----------------------------------------------------------------------------
79 // 숫자의 자리수(cnt)에 맞도록 반환
80 // @return : String
81 //-----------------------------------------------------------------------------
82 String.prototype.digits = function(cnt) {
83 var digit = "";
84 if (this.length < cnt) {
85 for(var i = 0; i < cnt - this.length; i++) {
86 digit += "0";
87 }
88 }
89 return digit + this;
90 }
91 //-----------------------------------------------------------------------------
92 // " -> " ' -> '로 바꾸어서 반환
93 // @return : String
94 //-----------------------------------------------------------------------------
95 String.prototype.quota = function() {
96 return this.replace(/"/g, """).replace(/'/g, "'");
97 }
98 //-----------------------------------------------------------------------------
99 // 파일 확장자만 가져오기
100 // @return : String
101 //-----------------------------------------------------------------------------
102 String.prototype.ext = function() {
103 return (this.indexOf(".") < 0) ? "" : this.substring(this.lastIndexOf(".") + 1, this.length);
104 }
105 //-----------------------------------------------------------------------------
106 // URL에서 파라메터 제거한 순수한 url 얻기
107 // @return : String
108 //-----------------------------------------------------------------------------
109 String.prototype.uri = function() {
110 var arr = this.split("?");
111 arr = arr[0].split("#");
112 return arr[0];
113 }
114
115 /*---------------------------------------------------------------------------------*\
116 * 각종 체크 함수들
117 \*---------------------------------------------------------------------------------*/
118 //-----------------------------------------------------------------------------
119 // 정규식에 쓰이는 특수문자를 찾아서 이스케이프 한다.
120 // @return : String
121 //-----------------------------------------------------------------------------
122 String.prototype.meta = function() {
123 var str = this;
124 var result = ""
125 for(var i = 0; i < str.length; i++) {
126 if((/([\$\(\)\*\+\.\[\]\?\\\^\{\}\|]{1})/).test(str.charAt(i))) {
127 result += str.charAt(i).replace((/([\$\(\)\*\+\.\[\]\?\\\^\{\}\|]{1})/), \\$1);
128 }
129 else {
130 result += str.charAt(i);
131 }
132 }
133 return result;
134 }
135 //-----------------------------------------------------------------------------
136 // 정규식에 쓰이는 특수문자를 찾아서 이스케이프 한다.
137 // @return : String
138 //-----------------------------------------------------------------------------
139 String.prototype.remove = function(pattern) {
140 return (pattern == null) ? this : eval("this.replace(/[" + pattern.meta() + "]/g, \"\")");
141 }
142 //-----------------------------------------------------------------------------
143 // 최소 최대 길이인지 검증
144 // str.isLength(min [,max])
145 // @return : boolean
146 //-----------------------------------------------------------------------------
147 String.prototype.isLength = function() {
148 var min = arguments[0];
149 var max = arguments[1] ? arguments[1] : null;
150 var success = true;
151 if(this.length < min) {
152 success = false;
153 }
154 if(max && this.length > max) {
155 success = false;
156 }
157 return success;
158 }
159 //-----------------------------------------------------------------------------
160 // 최소 최대 바이트인지 검증
161 // str.isByteLength(min [,max])
162 // @return : boolean
163 //-----------------------------------------------------------------------------
164 String.prototype.isByteLength = function() {
165 var min = arguments[0];
166 var max = arguments[1] ? arguments[1] : null;
167 var success = true;
168 if(this.byte() < min) {
169 success = false;
170 }
171 if(max && this.byte() > max) {
172 success = false;
173 }
174 return success;
175 }
176 //-----------------------------------------------------------------------------
177 // 공백이나 널인지 확인
178 // @return : boolean
179 //-----------------------------------------------------------------------------
180 String.prototype.isBlank = function() {
181 var str = this.trim();
182 for(var i = 0; i < str.length; i++) {
183 if ((str.charAt(i) != "\t") && (str.charAt(i) != "\n") && (str.charAt(i)!="\r")) {
184 return false;
185 }
186 }
187 return true;
188 }
189 //-----------------------------------------------------------------------------
190 // 숫자로 구성되어 있는지 학인
191 // arguments[0] : 허용할 문자셋
192 // @return : boolean
193 //-----------------------------------------------------------------------------
194 String.prototype.isNum = function() {
195 return (/^[0-9]+$/).test(this.remove(arguments[0])) ? true : false;
196 }
197 //-----------------------------------------------------------------------------
198 // 영어만 허용 - arguments[0] : 추가 허용할 문자들
199 // @return : boolean
200 //-----------------------------------------------------------------------------
201 String.prototype.isEng = function() {
202 return (/^[a-zA-Z]+$/).test(this.remove(arguments[0])) ? true : false;
203 }
204 //-----------------------------------------------------------------------------
205 // 숫자와 영어만 허용 - arguments[0] : 추가 허용할 문자들
206 // @return : boolean
207 //-----------------------------------------------------------------------------
208 String.prototype.isEngNum = function() {
209 return (/^[0-9a-zA-Z]+$/).test(this.remove(arguments[0])) ? true : false;
210 }
211 //-----------------------------------------------------------------------------
212 // 숫자와 영어만 허용 - arguments[0] : 추가 허용할 문자들
213 // @return : boolean
214 //-----------------------------------------------------------------------------
215 String.prototype.isNumEng = function() {
216 return this.isEngNum(arguments[0]);
217 }
218 //-----------------------------------------------------------------------------
219 // 아이디 체크 영어와 숫자만 체크 첫글자는 영어로 시작 - arguments[0] : 추가 허용할 문자들
220 // @return : boolean
221 //-----------------------------------------------------------------------------
222 String.prototype.isUserid = function() {
223 return (/^[a-zA-z]{1}[0-9a-zA-Z]+$/).test(this.remove(arguments[0])) ? true : false;
224 }
225 //-----------------------------------------------------------------------------
226 // 한글 체크 - arguments[0] : 추가 허용할 문자들
227 // @return : boolean
228 //-----------------------------------------------------------------------------
229 String.prototype.isKor = function() {
230 return (/^[가-힣]+$/).test(this.remove(arguments[0])) ? true : false;
231 }
232 //-----------------------------------------------------------------------------
233 // 주민번호 체크 - arguments[0] : 주민번호 구분자
234 // XXXXXX-XXXXXXX
235 // @return : boolean
236 //-----------------------------------------------------------------------------
237 String.prototype.isJumin = function() {
238 var arg = arguments[0] ? arguments[0] : "";
239 var jumin = eval("this.match(/[0-9]{2}[01]{1}[0-9]{1}[0123]{1}[0-9]{1}" + arg + "[1234]{1}[0-9]{6}$/)");
240 if(jumin == null) {
241 return false;
242 }
243 else {
244 jumin = jumin.toString().num().toString();
245 }
246 // 생년월일 체크
247 var birthYY = (parseInt(jumin.charAt(6)) == (1 ||2)) ? "19" : "20";
248 birthYY += jumin.substr(0, 2);
249 var birthMM = jumin.substr(2, 2) - 1;
250 var birthDD = jumin.substr(4, 2);
251 var birthDay = new Date(birthYY, birthMM, birthDD);
252 if(birthDay.getYear() % 100 != jumin.substr(0,2) || birthDay.getMonth() != birthMM || birthDay.getDate() != birthDD) {
253 return false;
254 }
255 var sum = 0;
256 var num = [2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5]
257 var last = parseInt(jumin.charAt(12));
258 for(var i = 0; i < 12; i++) {
259 sum += parseInt(jumin.charAt(i)) * num[i];
260 }
261 return ((11 - sum % 11) % 10 == last) ? true : false;
262 }
263 //-----------------------------------------------------------------------------
264 // 외국인 등록번호 체크 - arguments[0] : 등록번호 구분자
265 // XXXXXX-XXXXXXX
266 // @return : boolean
267 //-----------------------------------------------------------------------------
268 String.prototype.isForeign = function() {
269 var arg = arguments[0] ? arguments[0] : "";
270 var jumin = eval("this.match(/[0-9]{2}[01]{1}[0-9]{1}[0123]{1}[0-9]{1}" + arg + "[5678]{1}[0-9]{1}[02468]{1}[0-9]{2}[6789]{1}[0-9]{1}$/)");
271 if(jumin == null) {
272 return false;
273 }
274 else {
275 jumin = jumin.toString().num().toString();
276 }
277 // 생년월일 체크
278 var birthYY = (parseInt(jumin.charAt(6)) == (5 || 6)) ? "19" : "20";
279 birthYY += jumin.substr(0, 2);
280 var birthMM = jumin.substr(2, 2) - 1;
281 var birthDD = jumin.substr(4, 2);
282 var birthDay = new Date(birthYY, birthMM, birthDD);
283 if(birthDay.getYear() % 100 != jumin.substr(0,2) || birthDay.getMonth() != birthMM || birthDay.getDate() != birthDD) {
284 return false;
285 }
286 if((parseInt(jumin.charAt(7)) * 10 + parseInt(jumin.charAt(8))) % 2 != 0) {
287 return false;
288 }
289 var sum = 0;
290 var num = [2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5]
291 var last = parseInt(jumin.charAt(12));
292 for(var i = 0; i < 12; i++) {
293 sum += parseInt(jumin.charAt(i)) * num[i];
294 }
295 return (((11 - sum % 11) % 10) + 2 == last) ? true : false;
296 }
297 //-----------------------------------------------------------------------------
298 // 사업자번호 체크 - arguments[0] : 등록번호 구분자
299 // XX-XXX-XXXXX
300 // @return : boolean
301 //-----------------------------------------------------------------------------
302 String.prototype.isBiznum = function() {
303 var arg = arguments[0] ? arguments[0] : "";
304 var biznum = eval("this.match(/[0-9]{3}" + arg + "[0-9]{2}" + arg + "[0-9]{5}$/)");
305 if(biznum == null) {
306 return false;
307 }
308 else {
309 biznum = biznum.toString().num().toString();
310 }
311 var sum = parseInt(biznum.charAt(0));
312 var num = [0, 3, 7, 1, 3, 7, 1, 3];
313 for(var i = 1; i < 8; i++) sum += (parseInt(biznum.charAt(i)) * num[i]) % 10;
314 sum += Math.floor(parseInt(parseInt(biznum.charAt(8))) * 5 / 10);
315 sum += (parseInt(biznum.charAt(8)) * 5) % 10 + parseInt(biznum.charAt(9));
316 return (sum % 10 == 0) ? true : false;
317 }
318 //-----------------------------------------------------------------------------
319 // 법인 등록번호 체크 - arguments[0] : 등록번호 구분자
320 // XXXXXX-XXXXXXX
321 // @return : boolean
322 //-----------------------------------------------------------------------------
323 String.prototype.isCorpnum = function() {
324 var arg = arguments[0] ? arguments[0] : "";
325 var corpnum = eval("this.match(/[0-9]{6}" + arg + "[0-9]{7}$/)");
326 if(corpnum == null) {
327 return false;
328 }
329 else {
330 corpnum = corpnum.toString().num().toString();
331 }
332 var sum = 0;
333 var num = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
334 var last = parseInt(corpnum.charAt(12));
335 for(var i = 0; i < 12; i++) {
336 sum += parseInt(corpnum.charAt(i)) * num[i];
337 }
338 return ((10 - sum % 10) % 10 == last) ? true : false;
339 }
340 //-----------------------------------------------------------------------------
341 // 이메일의 유효성을 체크
342 // @return : boolean
343 //-----------------------------------------------------------------------------
344 String.prototype.isEmail = function() {
345 return (/\w+([-+.]\w+)*@\w+([-.]\w+)*\.[a-zA-Z]{2,4}$/).test(this.trim());
346 }
347 //-----------------------------------------------------------------------------
348 // 전화번호 체크 - arguments[0] : 전화번호 구분자
349 // @return : boolean
350 //-----------------------------------------------------------------------------
351 String.prototype.isPhone = function() {
352 var arg = arguments[0] ? arguments[0] : "";
353 return eval("(/(02|0[3-9]{1}[0-9]{1})" + arg + "[1-9]{1}[0-9]{2,3}" + arg + "[0-9]{4}$/).test(this)");
354 }
355 //-----------------------------------------------------------------------------
356 // 핸드폰번호 체크 - arguments[0] : 핸드폰 구분자
357 // @return : boolean
358 //-----------------------------------------------------------------------------
359 String.prototype.isMobile = function() {
360 var arg = arguments[0] ? arguments[0] : "";
361 return eval("(/01[016789]" + arg + "[1-9]{1}[0-9]{2,3}" + arg + "[0-9]{4}$/).test(this)");
362 }
'javascript' 카테고리의 다른 글
자바스크립트 정규표현식 (4) | 2007.11.28 |
---|---|
자바스크립트 성능 향상 팁 (0) | 2007.11.23 |
유용한 자바스크립트 (0) | 2007.11.19 |
javascript의 eval()함수란 무엇인가! (0) | 2007.11.12 |
자바 스크립트 내장 객체 오버라이딩 (javascript prototype overriding) (1) | 2007.11.12 |
'javascript'의 다른글
- 현재글String Function모음 String을 자바처럼 핸들링해보잣~!