/* sky.form.checker.js 2007-04-18 ~ 2008-06-02 Seo, Jaehan @dependence sky.base.js [ SYNOPSYS ] function init() { var fc = new Form.Checker({ form : 'formIdOrName', message : '%s in invalid.', // optional. default message format preFunc : preCheckFunction, // optional. postFunc : postCheckFunction, // optional. }); // add check list fc.check('age', /^[0-9]+$/, 'Age', '%s must be number.'); // RegExp object fc.check('email', checkEmail, 'Email'); // function fc.check('passwd', 'length:4,8', 'Password'); // option string // for lib.validate.js style fc.scan(); } $l(window, 'load', init); [ options for Form.Checker.add() ] - RegExp object - function - option string . length:[min,]max - string length . byte:[min,]max - string byte . range:[min,]max - number range . check:[min,]max - checkbox check count // add . select:[min,]max - selectbox select count // add . alter[:max] - alternative . glue:string - glue . empty - check if empty. (default) . trim - trim value (not applied to original) . apply:element - apply value to another element . match:element - compare elements value . email, homepage, url, jumin, bizno, phone, mobile, number, date - pre defined check function */ if (typeof Form == 'undefined') { Form = {}; } if (typeof Form.Checker == 'undefined') { Form.Checker = Class({ init : function(options) { Class.extend(this, { form : null, auto : true, message : '%s ' + _T('{{LANG_001:항목이 바르지 않습니다.}}'), preFunc : null, postFunc : null, isSubmitting: false // 중복 방지 플래그 추가 }, options); this.checkList = {}; this.funcList = { 'email' : this.checkEmail, 'homepage' : this.checkHomepate, 'url' : this.checkUrl, 'jumin' : this.checkJumin, 'foreign' : this.checkForeign, 'bizno' : this.checkBizNo, 'phone' : this.checkPhone, 'mobile' : this.checkMobile, 'number' : this.checkNumber, 'date' : this.checkDate } if (this.setForm(this.form)) { this.onCheck = this.onSubmit.bindForEvent(this); if (this.auto) { $l(this.form, 'submit', this.onCheck); } } }, setForm : function(form) { if (typeof form == 'string') { this.form = $form(form); } else if (typeof form.tagName == 'string' && form.tagName.toLowerCase() == 'form') { this.form = form; } else { this.form = null; } if (this.form) { if (typeof this.form.disableChecker !== 'undefined') { return false; } this.form.Checker = this; this.form.setAttribute("autocomplete", "off"); // for Firefox forcus exception error } return this.form; }, addCheck : function(element, option, name, message, enable) { element = this.getElement(element); if (!element) { return false; // error } var key = element.id || element.name || element[0].id || element[0].name; if (!key) { return false; // error } if (this.checkList[key]) { this.checkList[key]['enable'] = true; } else { this.checkList[key] = { enable : (enable === false) ? false : true, element : element, option : this.parseOption(option), name : name, message : message || this.message }; } return true; }, delCheck : function(element) { element = this.getElement(element); var key = element.id || element.name || element[0].id || element[0].name; if (this.checkList[key]) { this.checkList[key]['enable'] = false; } }, toggle : function(element) { element = this.getElement(element); var key = element.id || element.name || element[0].id || element[0].name; if (this.checkList[key]) { this.checkList[key]['enable'] = !this.checkList[key]['enable']; } }, onSubmit : function(evt) { if (typeof this.form.disableChecker !== 'undefined') { return; } // 중복 클릭 방지 if (this.isSubmitting) { Event.stop(evt); return; } this.isSubmitting = true; // 전송 버튼 비활성화 var submitBtn = this.form.querySelector('button[type="submit"], input[type="submit"]'); if (submitBtn) { submitBtn.disabled = true; } Event.stop(evt); this.doSubmit(); }, doSubmit : function() { if (this.doCheck()) { if (typeof this.onSuccess == 'function') { this.onSuccess.call(this); } else { if (this.form.useSSL) { if(typeof this.form.useAjax != 'undefined' && this.form.useAjax){ this.doSSLSubmitAjax(); }else{ // apply SSL this.doSSLSubmit(); } } else { this.form.submit(); } } } }, doSSLSubmit : function() { var list = this.form.elements; for (var i=0; i 1) { if (typeof check.option.glue != 'undefined') { value = values.join(check.option.glue); } else { value = values; } } else { value = values[0] || ''; } } else { value = values[0] = $v(element); } var flag = true; for (var key in check.option) { if (!check.option.hasOwnProperty(key)) { continue; } var option = check.option[key]; // RegExp if (option instanceof RegExp) { flag = option.test(value); // funciton } else if (typeof option == 'function') { flag = option(value, element); // pre defined function } else if (typeof this.funcList[key] == 'function') { flag = this.funcList[key](value, element); // user function name } else if (key == 'func') { var func = eval(option); if (typeof func == 'function') { flag = func(value, element); } else { // invalid user function // TODO } // length, byte, range, check } else if (/^(length|byte|range|check|select)$/.test(key)) { switch (key) { case 'byte' : var cmp = this.getByte(value); break; case 'range' : var cmp = parseInt(value, 10) || 0; break; default : var cmp = value.length; } if (option.length == 1) { option[0] = parseInt(option[0], 10); flag = (cmp <= option[0]) ? true : false; } else if (option.length >= 2) { option[0] = parseInt(option[0], 10); option[1] = parseInt(option[1], 10); flag = ((!option[0] || cmp >= option[0]) && (!option[1] || cmp <= option[1])) ? true : false; } // etc } else { switch (key) { case 'empty' : for (var j=0; j= 128) { byteTotal += 2; } else { byteTotal++; } } return byteTotal; }, parseOption : function(option) { if (!(option instanceof Array)) option = [option]; var result = {}; var index = 0; for (var i=0; i 0) { for (var i=x+1; i 0) { for (var i=0; i= 10) sum -= 10; sum += 2; if(sum >= 10) sum -= 10; return sum == last ? true : false; }, checkBizNo : function(value) { if (value instanceof Array) { value = value.join('-'); } if (!/(^[0-9]{3})-?([0-9]{2})-?([0-9]{5}$)/.test(value)) return false; num = value.replace(/[^0-9]/g, ''); var cVal = 0; for (var i=0; i<8; i++) { var cKeyNum = parseInt(((_tmp = i % 3) == 0) ? 1 : ( _tmp == 1 ) ? 3 : 7); cVal += (parseFloat(num.substring(i,i+1)) * cKeyNum) % 10; } var li_temp = parseFloat(num.substring(i,i+1)) * 5 + "0"; cVal += parseFloat(li_temp.substring(0,1)) + parseFloat(li_temp.substring(1,2)); var last_checknum = (cVal % 10) % 10; last_checknum = last_checknum > 0 ? 10 - last_checknum : last_checknum; return (parseInt(num.substring(9,10)) == last_checknum) ? true : false; }, checkPhone : function(value) { if (value instanceof Array) { value = value.join('-'); } return (/^([0][0-9]{1,2})-?([1-9][0-9]{2,3})-?([0-9]{4})$/.test(value)) ? true : false; }, checkMobile : function(value) { if (value instanceof Array) { value = value.join('-'); } return (/^(01[016-9])-?([1-9][0-9]{2,3})-?([0-9]{4})$/.test(value)) ? true : false; }, checkNumber : function(value) { if (value instanceof Array) { value = value[0]; } return (/^[0-9]+$/.test(value)) ? true : false; }, checkDate : function(value) { if (value instanceof Array) { value = value.join('-'); } // YYYY-MM-DD only value = value.replace(/[^0-9]/, '-'); if (!/^[12][0-9]{3}-[0-9]{2}-[0-9]{2}$/.test(value)) { return false; } var tmp = value.split('-'); var date = new Date(tmp[0], tmp[1]-1, tmp[2]); return (value == date.getFullYear() + '-' + ('0'+(date.getMonth()+1)).slice(-2) + '-' + ('0'+date.getDate()).slice(-2)) ? true : false; } }); Form.Checker.prototype.check = Form.Checker.prototype.addCheck; }