PageSourceSearch

https://www.careet.net/content/Scripts/univ.validator.js

js careet.net collected 2026-10-02 11:51:44 UTC 22,654 bytes, 611 lines download raw bytes

1// ------------------------------------------------------------------------------------------------
2// - 폼체크 기능 (written by luckshim)
3// ------------------------------------------------------------------------------------------------
4/**
5validator.checkIsNumber("ctbCoCd", "CoCd 를 입력해 주세요1.", true);
6validator.checkBetweenNum("ctbCoCd", 1, 3, "1-3 사이의 글을 입력해주세요", true);
7validator.checkContains("ctbGLAct", "test", "test 라는 글자는 금칙어입니다.", true);
8validator.checkSelected("ddlCtye", 1, "두번째부터 선택하세요.", true);
9validator.checkWhiteSpace("ctbCoCd", "공백을 입력하시면 안됩니다.", true);
10validator.checkMaxLength("ctbCoCd", 5, "5자 초과하면 안됩니다.", true);
11validator.checkMinLength("ctbCoCd", 5, "5자 미만이면 안됩니다.", true);
12validator.checkBetweenLength("ctbCoCd", 1,5, "1~5자 이내여야 합니다.", true);
13*/
14
15/**
16* 폼체크 클래스 - checkForm 없을 경우 id로 체크함 - alertFunc 없을 경우 alert() 경고창
17* 
18* @param {form object} checkForm 체크할 폼
19* @param {function object} alertFunc 경고창을 표시할 함수
20*/
21Validator = function (checkForm, alertFunc) {
22    this.checkForm = checkForm;
23    this.validateObjectArray = new Array();
24    this.alertFunc = alertFunc;  // 경고창 함수
25}
26
27/**
28* 유효성 객체
29* 
30* @param {string} identifier id 또는 name
31* @param {string} errorMessage 에러메시지
32* @param {boolean} focusFlag 포커스여부
33*/
34Validator.prototype.ValidateObject = function (identifier, errorMessage, focusFlag) {
35    this.identifier = identifier;
36    this.errorMessage = errorMessage;
37    this.focusFlag = focusFlag;
38}
39
40/**
41* id 또는 name으로 엘리먼트 객체를 획득
42* 
43* @param {string} identifier id 또는 name
44* @return {element object} 엘리먼트 객체
45*/
46Validator.prototype.getElement = function (identifier) {
47    if (this.checkForm && this.checkForm.nodeName == "FORM") {
48        return this.checkForm[identifier];
49    } else {
50        return document.getElementById(identifier);
51    }
52}
53
54/**
55* 유효성 검사
56* 
57* @return {boolean} 유효성 검사결과
58*/
59Validator.prototype.validate = function () {
60    for (var i = 0; i < this.validateObjectArray.length; i++) {
61        var validateObject = this.validateObjectArray[i];
62        if (!validateObject.validate()) {
63            if (this.alertFunc) this.alertFunc(validateObject.errorMessage);
64            else alert(validateObject.errorMessage);
65            if (validateObject.focusFlag) {
66                this.getElement(validateObject.identifier).focus();
67            }
68            this.validateObjectArray = new Array();
69            return false;
70        }
71    }
72    this.validateObjectArray = new Array();
73    return true;
74}
75
76/**
77* 값을 입력했는지의 여부를 검사
78* 
79* @param {string} identifier id 또는 name
80* @param {string} errorMessage 에러메시지
81* @param {boolean} focusFlag 포커스여부
82*/
83Validator.prototype.checkRequired = function (identifier, errorMessage, focusFlag) {
84    var checkElement = $("#" + identifier);
85    var validateObject = new this.ValidateObject(identifier, errorMessage, focusFlag);
86    validateObject.validate = function () {
87        return (checkElement.val()).replace(/^\s*/, "").replace(/\s*$/, "") != "";
88    }
89    this.validateObjectArray.push(validateObject);
90}
91
92/**
93* 값의 길이가 maxLength보다 작거나 같은지 검사
94* 
95* @param {string} identifier id 또는 name
96* @param {int} maxLength 체크값
97* @param {string} errorMessage 에러메시지
98* @param {boolean} focusFlag 포커스여부
99*/
100Validator.prototype.checkMaxLength = function (identifier, maxLength, errorMessage, focusFlag) {
101    var checkElement = $("#" + identifier);
102    var validateObject = new this.ValidateObject(identifier, errorMessage, focusFlag);
103    validateObject.validate = function () {
104        var stringValue = checkElement.val();
105        //alert("callme " + stringValue.length <= maxLength);
106        return (stringValue.length <= maxLength);
107    }
108    this.validateObjectArray.push(validateObject);
109}
110
111/**
112* 값의 길이가 minLength보다 크거나 같은지 검사
113* 
114* @param {string} identifier id 또는 name
115* @param {int} minLength 체크값
116* @param {string} errorMessage 에러메시지
117* @param {boolean}
117 focusFlag 포커스여부
118*/
119Validator.prototype.checkMinLength = function (identifier, minLength, errorMessage, focusFlag) {
120    var checkElement = $("#" + identifier);
121    var validateObject = new this.ValidateObject(identifier, errorMessage, focusFlag);
122    validateObject.validate = function () {
123        var stringValue = checkElement.val();
124        return (minLength <= stringValue.length);
125    }
126    this.validateObjectArray.push(validateObject);
127}
128
129/**
130* 값의 길이가 minLength와 maxLength사이에 있는지 검사
131* 
132* @param {string} identifier id 또는 name
133* @param {int} minLength 체크값
134* @param {int} maxLength 체크값
135* @param {string} errorMessage 에러메시지
136* @param {boolean} focusFlag 포커스여부
137*/
138Validator.prototype.checkBetweenLength = function (identifier, minLength, maxLength, errorMessage, focusFlag) {
139    var checkElement = $("#" + identifier);
140    var validateObject = new this.ValidateObject(identifier, errorMessage, focusFlag);
141    validateObject.validate = function () {
142        var stringValue = checkElement.val();
143        return (minLength <= stringValue.length && stringValue.length <= maxLength);
144    }
145    this.validateObjectArray.push(validateObject);
146}
147
148/**
149* 값의 바이트 길이가 maxLength보다 작거나 같은지 검사
150* 
151* @param {string} identifier id 또는 name
152* @param {int} maxLength 체크값
153* @param {int} kbyte 한글바이트수
154* @param {string} errorMessage 에러메시지
155* @param {boolean} focusFlag 포커스여부
156*/
157Validator.prototype.checkMaxLengthByte = function (identifier, maxLength, kbyte, errorMessage, focusFlag) {
158    var checkElement = $("#" + identifier);
159    var validateObject = new this.ValidateObject(identifier, errorMessage, focusFlag);
160    validateObject.validate = function () {
161        var stringValue = checkElement.val();
162        var nbytes = 0;
163        for (i = 0; i < stringValue.length; i++) {
164            var ch = stringValue.charAt(i);
165            if (escape(ch).length > 4) {
166                nbytes += kbyte;
167            } else if (ch == '\n') {
168                if (stringValue.charAt(i - 1) != '\r') {
169                    nbytes += 1;
170                }
171            } else if (ch == '<' || ch == '>') {
172                nbytes += 4;
173            } else {
174                nbytes += 1;
175            }
176        }
177        return (nbytes <= maxLength);
178    }
179    this.validateObjectArray.push(validateObject);
180}
181
182/**
183* 값의 바이트 길이가 minLength보다 작거나 같은지 검사
184* 
185* @param {string} identifier id 또는 name
186* @param {int} minLength 체크값
187* @param {int} kbyte 한글바이트수
188* @param {string} errorMessage 에러메시지
189* @param {boolean} focusFlag 포커스여부
190*/
191Validator.prototype.checkMinLengthByte = function (identifier, minLength, kbyte, errorMessage, focusFlag) {
192    var checkElement = $("#" + identifier);
193    var validateObject = new this.ValidateObject(identifier, errorMessage, focusFlag);
194    validateObject.validate = function () {
195        var stringValue = checkElement.val();
196        var nbytes = 0;
197        for (i = 0; i < stringValue.length; i++) {
198            var ch = stringValue.charAt(i);
199            if (escape(ch).length > 4) {
200                nbytes += kbyte;
201            } else if (ch == '\n') {
202                if (stringValue.charAt(i - 1) != '\r') {
203                    nbytes += 1;
204                }
205            } else if (ch == '<' || ch == '>') {
206                nbytes += 4;
207            } else {
208                nbytes += 1;
209            }
210        }
211        return (nbytes >= minLength);
212    }
213    this.validateObjectArray.push(validateObject);
214}
215
216/**
217* 값의 바이트 길이가 minLength와 maxLength사이에 있는지 검사
218* 
219* @param {string} identifier id 또는 name
220* @param {int} minLength 체크값
221* @param {int} maxLength 체크값
222* @param {int} kbyte 한글바이트수
223* @param {string} errorMessage 에러메시지
224* @param {boolean} focusFlag 포커스여부
225*/
226Validator.prototype.checkBetweenLengthByte = function (identifier, minLength, maxLength, kbyte, errorMessage, focusFlag) {
227    var checkElement = $("#" + identifier);
228    var validateObject = new this.ValidateObject(identifier, errorMessage, focusFlag);
229    validateObject.validate = function () {
230        var stringValue = checkElement.val();
231        var nbytes = 0;
232        for (i = 0; i < stringValue.length; i++) {
233            var ch = stringValue.charAt(i);
234            if (escape(ch).length > 4) {
235                nbytes += kbyte;
236            } else if (ch == '\n') {
237                if (stringValue.charAt(i - 1) != '\r') {
238                    nbytes += 1;
239                }
240            } else if (ch == '<' || ch == '>') {
241                nbytes += 4;
242            } else {
243                nbytes += 1;
244            }
245        }
246        return (minLength <= nbytes && nbytes <= maxLength);
247    }
248    this.validateObjectArray.push(validateObject);
249}
250
251/**
252* 값이 정규표현식에 해당하는 지 검사
253* 
254* @param {string} identifier id 또는 name
255* @param {string} regex 정규표현식
256* @param {boolean} includeFlag 정규표현식 해당여부 포함되어야할때 true
257* @param {boolean} focusFlag 포커스여부
258*/
259Validator.prototype.checkRegex = function (identifier, regex, includeFlag, errorMessage, focusFlag) {
260    var checkElement = $("#" + identifier);
261    var validateObject = new this.ValidateObject(identifier, errorMessage, focusFlag);
262    validateObject.validate = function () {
263        var str = checkElement.val();
264        if (str.length == 0) return true;
265        if (includeFlag) return str.search(regex) != -1;
266        else return str.search(regex) == -1;
267    }
268    this.validateObjectArray.push(validateObject);
269}
270
271/**
272* 값이 알파벳과 0~9사이의 숫자만 포함하는 지 검사
273* 
274* @param {string} identifier id 또는 name
275* @param {string} errorMessage 에러메시지
276* @param {boolean} focusFlag 포커스여부
277*/
278Validator.prototype.checkAlphaNum = function (identifier, errorMessage, focusFlag) {
279    this.checkRegex(identifier, /^[a-zA-Z0-9]+$/, true, errorMessage, focusFlag);
280}
281
282/**
283* 값이 0~9 사이의 문자만 포함하는 지 검사
284* 
285* @param {string} identifier id 또는 name
286* @param {string} errorMessage 에러메시지
287* @param {boolean} focusFlag 포커스여부
288*/
289Validator.prototype.checkOnlyNumber = function (identifier, errorMessage, focusFlag) {
290    this.checkRegex(identifier, /^[0-9]+$/, true, errorMessage, focusFlag);
291}
292
293/**
294* 영대소문자만 입력인지 검사
295* 
296* @param {string} identifier id 또는 name
297* @param {string} errorMessage 에러메시지
298* @param {boolean} focusFlag 포커스여부
299*/
300Validator.prototype.checkOnlyAlpabat = function (identifier, errorMessage, focusFlag) {
301    this.checkRegex(identifier, /^[A-za-z]+$/, true, errorMessage, focusFlag);
302}
303
304/**
305* 값이 올바른 이메일 주소인지 검사
306* 
307* @param {string} identifier id 또는 name
308* @param {string} errorMessage 에러메시지
309* @param {boolean} focusFlag 포커스여부
310*/
311Validator.prototype.checkEmail = function (identifier, errorMessage, focusFlag) {
312    this.checkRegex(identifier, /^((\w|[\-\.])+)@((\w|[\-\.])+)\.([A-Za-z]+)$/, true, errorMessage, focusFlag);
313}
314
315/**
316* 올바른 주소인지 검사
317* 
318* @param {string} identifier id 또는 name
319* @param {string} errorMessage 에러메시지
320* @param {boolean} focusFlag 포커스여부
321*/
322Validator.prototype.checkUrl = function (identifier, errorMessage, focusFlag) {
323    this.checkRegex(identifier, /^(?:(?:ftp|https?):\/\/)?(?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)+(?:com|edu|biz|org|gov|int|info|mil|net|name|museum|coop|aero|[a-z][a-z])\b(?:\d+)?(?:\/[^;"'<>()\[\]{}\s\x7f-\xff]*(?:[.,?]+[^;"'<>()\[\]{}\s\x7f-\xff]+)*)?/, true, errorMessage, focusFlag);
324}
325
326/**
327* 특수문자 검사
328* 
329* @param {string} identifier id 또는 name
330* @param {string} errorMessage 에러메시지
331* @param {boolean} focusFlag 포커스여부
332*/
333Validator.prototype.checkSpecialChar = function (identifier, errorMessage, focusFlag) {
334    this.checkRegex(identifier, /[~!@\#$%^&*\()\-=+_']/, false, errorMessage, focusFlag);
335}
336
337/**
338* 공백 검사
339* 
340* @param {string} identifier id 또는 name
341* @param {string} errorMessage 에러메시지
342* @param {boolean}
342 focusFlag 포커스여부
343*/
344Validator.prototype.checkWhiteSpace = function (identifier, errorMessage, focusFlag) {
345    this.checkRegex(identifier, /\s/g, false, errorMessage, focusFlag);
346}
347
348/**
349* <select>에서 선택한 옵션의 인덱스가 firstIdx보다 크거나 같은 지 검사한다.
350* 
351* @param {string} identifier id 또는 name
352* @param {int} firstIdx 옵션인덱스
353* @param {string} errorMessage 에러메시지
354* @param {boolean} focusFlag 포커스여부
355*/
356Validator.prototype.checkSelected = function (identifier, firstIdx, errorMessage, focusFlag) {
357    var checkElement = this.getElement(identifier);
358    var validateObject = new this.ValidateObject(identifier, errorMessage, focusFlag);
359    validateObject.validate = function () {
360        var idx = checkElement.selectedIndex;
361        return idx >= firstIdx;
362    }
363    this.validateObjectArray.push(validateObject);
364}
365
366/**
367* checkbox나 radio 입력 요소가 최소한 1개 이상 선택됐는지 검사
368* 
369* @param {string} identifier id 또는 name
370* @param {string} errorMessage 에러메시지
371* @param {boolean} focusFlag 포커스여부
372*/
373Validator.prototype.checkAtLeastOneChecked = function (identifier, errorMessage, focusFlag) {
374    var validateObject = new this.ValidateObject(null, errorMessage, false);
375    validateObject.validate = function () {
376        var ele = document.getElementsByName(identifier);
377        for (var idxe = 0; idxe < ele.length; idxe++) {
378            if (ele[idxe].checked == true) return true;
379        }
380        return false;
381    }
382    this.validateObjectArray.push(validateObject);
383}
384
385/**
386* checkboxlist 입력 요소가 1개 이상 선택됐는지 검사
387* 
388* @param {string} identifier id 또는 name
389* @param {string} errorMessage 에러메시지
390* @param {boolean} focusFlag 포커스여부
391*/
392Validator.prototype.checkboxListChecked = function (identifier, errorMessage, focusFlag) {
393
394    var validateObject = new this.ValidateObject(null, errorMessage, false);
395    validateObject.validate = function () {
396        var res = $("[id^=" + identifier + "]:checkbox:checked").length > 0;
397        return res;
398    }
399    this.validateObjectArray.push(validateObject);
400}
401
402/**
403* 특정 문자열이 포함되었는지 검사
404* 
405* @param {string} identifier id 또는 name
406* @param {string} checkContainText 포함 문자열
407* @param {string} errorMessage 에러메시지
408* @param {boolean} focusFlag 포커스여부
409*/
410Validator.prototype.checkContains = function (identifier, checkContainText, errorMessage, focusFlag) {
411    var checkElement = $("#" + identifier);
412    var validateObject = new this.ValidateObject(identifier, errorMessage, false);
413    validateObject.validate = function () {
414        return checkElement.val().indexOf(checkContainText) == -1;
415    }
416    this.validateObjectArray.push(validateObject);
417}
418
419/**
420* 입력된 숫자가 지정된 범위의 숫자인지 체크
421* 
422* @param {string} identifier id 또는 name
423* @param {int} minNum 지정 최소 수
424* @param {int} maxNum 지정 최대 수
425* @param {string} errorMessage 에러메시지
426* @param {boolean} focusFlag 포커스여부
427*/
428Validator.prototype.checkBetweenNum = function (identifier, minNum, maxNum, errorMessage, focusFlag) {
429    var checkElement = $("#" + identifier);
430    var validateObject = new this.ValidateObject(identifier, errorMessage, false);
431    validateObject.validate = function () {
432        var number = checkElement.val();
433        return (number >= minNum && number <= maxNum);
434    }
435    this.validateObjectArray.push(validateObject);
436}
437
438/**
439* isNaN 함수로 숫자여부 체크
440* 
441* @param {string} identifier id 또는 name
442* @param {string} errorMessage 에러메시지
443* @param {boolean} focusFlag 포커스여부
444*/
445Validator.prototype.checkIsNumber = function (identifier, errorMessage, focusFlag) {
446    var checkElement = $("#" + identifier);
447    var validateObject = new this.ValidateObject(identifier, errorMessage, false);
448    validateObject.validate = function () {
449        var val = checkElement.val();
450        return !isNaN(val);
451    }
452    this.validateObjectArray.push(validateObject);
453}
454
455/**
456* 콤마(,)를 제거후 isNaN 함수로 금액여부 체크
457* 
458* @param {string} identifier id 또는 name
459* @param {string} errorMessage 에러메시지
460* @param {boolean} focusFlag 포커스여부
461*/
462Validator.prototype.checkIsMoney = function (identifier, errorMessage, focusFlag) {
463    var checkElement = $("#" + identifier);
464    var validateObject = new this.ValidateObject(identifier, errorMessage, false);
465    validateObject.validate = function () {
466        var val = checkElement.val().replace(/,/g, "");
467        return !isNaN(val);
468    }
469    this.validateObjectArray.push(validateObject);
470}
471
472/**
473* <select>에서 선택한 옵션의 값을 확인한다.
474* 
475* @param {string} identifier id 또는 name
476* @param {int} firstIdx 옵션인덱스
477* @param {string} errorMessage 에러메시지
478* @param {boolean} focusFlag 포커스여부
479*/
480Validator.prototype.checkSelectedValue = function (identifier, defaultValue, errorMessage, focusFlag) {
481    var checkElement = this.getElement(identifier);
482    var validateObject = new this.ValidateObject(identifier, errorMessage, focusFlag);
483    validateObject.validate = function () {
484        var nowSelectedValue = $(checkElement).val();
485        return (nowSelectedValue == defaultValue ? false : true)
486    }
487    this.validateObjectArray.push(validateObject);
488}
489
490
491/**
492* 주민번호의 유효성을 검사한다.
493* 
494* @param {string} identifier id 또는 name
495* @param {string} errorMessage 에러메시지
496* @param {boolean} focusFlag 포커스여부
497*/
498Validator.prototype.checkRes = function (identifier, errorMessage, focusFlag) {
499    var checkElement = $("#" + identifier);
500    var validateObject = new this.ValidateObject(identifier, errorMessage, focusFlag);
501    validateObject.validate = function () {
502        var ssn = checkElement.val();
503        ssn = ssn.replace(/-/g, "");
504
505        var sum = 0;
506        var month = ssn.substr(2, 2);
507        var day = ssn.substr(4, 2);
508
509        if (ssn.length != 13) {
510            return false;
511        }
512
513        if (ssn.substr(7, 5) == "00000") {
514            return false;
515        }
516
517        var checkBit = new Array(2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5);
518        var num7 = ssn.charAt(6);
519        var num13 = ssn.charAt(12);
520        var total = 0;
521        if (ssn.length == 13) {
522            for (i = 0; i < checkBit.length; i++) { // 주민번호 12자리를 키값을 곱하여 합산한다.
523                total += ssn.charAt(i) * checkBit[i];
524            }
525            // 외국인 구분 체크
526            if (num7 == 0 || num7 == 9) { // 내국인 ( 1800년대 9: 남자, 0:여자)
527                total = (11 - (total % 11)) % 10;
528            }
529            else if (num7 > 4) {  // 외국인 ( 1900년대 5:남자 6:여자  2000년대 7:남자, 8:여자)
530                total = (13 - (total % 11)) % 10;
531            }
532            else { // 내국인 ( 1900년대 1:남자 2:여자  2000년대 3:남자, 4:여자)
533                total = (11 - (total % 11)) % 10;
534            }
535
536            if (total != num13) {
537                return false;
538            }
539            return true;
540        } else {
541            return false;
542        }
543    }
544    this.validateObjectArray.push(validateObject);
545}
546
547
548
549/**
550* 사업자등록번호의 유효성을 검사한다.
551* 
552* @param {string} identifier id 또는 name
553* @param {string} errorMessage 에러메시지
554* @param {boolean} focusFlag 포커스여부
555*/
556Validator.prototype.checkCompanyNumber = function (identifier, errorMessage, focusFlag) {
557    var checkElement = $("#" + identifier);
558    var validateObject = new this.ValidateObject(identifier, errorMessage, focusFlag);
559    validateObject.validate = function () {
560
561        var compNo = checkElement.val().replace(/-/g, "");
562
563        if (compNo == "9999999999" || compNo == "8888888888") { //해외거래처, 개인사업자
564            return true;
565        }
566
567        var sum = 0;
568        var getlist = new Array(10);
569        var chkvalue = new Array("1", "3", "7", "1", "3", "7", "1", "3", "5");
570        for (var i = 0; i < 10; i++) { getlist[i] = compNo.substring(i, i + 1); }
571        for (var i = 0; i < 9; i++) { sum += getlist[i] * chkvalue[i]; }
572        sum = sum + parseInt((getlist[8] * 5) / 10);
573        sidliy = sum % 10;
574        sidchk = 0;
575        if (sidliy != 0) { sidchk = 10 - sidliy; }
576        else { sidchk = 0; }
577        if (sidchk != getlist[9]) { return false; }
578        return true;
579    }
580    this.validateObjectArray.push(validateObject);
581}
582
583
584
585
586/**
587* ()를 제외한 특수문자 금지 
588* @param {string} identifier id 또는 name
589* @param {string} errorMessage 에러메시지
590* @param {boolean}
590 focusFlag 포커스여부
591*/
592Validator.prototype.checkSpecialCharNotIncParenthesis = function (identifier, errorMessage, focusFlag) {
593    var checkElement = $("#" + identifier);
594    var validateObject = new this.ValidateObject(identifier, errorMessage, focusFlag);
595    validateObject.validate = function () {
596
597        var regexp = /[0-9a-zA-Zㄱ-ㅎ|ㅏ-ㅣ|가-힣()-&]/;
598        var text = checkElement.val();
599
600        var b = true;
601        for (var i = 0 ; i < text.length; i++) {
602            if (text.charAt(i) != " " && regexp.test(text.charAt(i)) == false) {
603                b = false;
604                break;
605            }
606        }
607        return b;
608    }
609    this.validateObjectArray.push(validateObject);
610
611}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.