PageSourceSearch

https://innobranch.com/resources/common/js/form_checker.js

js innobranch.com collected 2026-10-08 08:26:12 UTC 9,833 bytes, 347 lines download raw bytes

1
2(function($) {
3    
4    $.fn.inputValidate = function()
5    {
6        const defaultSettings = {
7            rules: {
8                "required": {
9                    errorMessage: " ",
10                    fnChecking: function(value) {
11                        return (value && (value.trim().length > 0));
12                    }
13                },
14                "integer": {
15                    errorMessage: " ",
16                    fnChecking: function(value) {
17                        if (!value || value.trim().length == 0)
18                        {
19                            return true;
20                        }
21
22                        return value.match(/^\d+$/);
23                    }
24                },
25                "yyyy-mm-dd": {
26                    errorMessage: "yyyy-mm-dd 형식으로 입력하세요.",
27                    fnChecking: function(value) {
28                        if (!value || value.trim().length == 0)
29                        {
30                            return true;
31                        }
32
33                        return value.match(/^\d{4}\-(0?[1-9]|1[012])\-(0[1-9]|[12]\d|3[01])$/);
34                    }
35                },
36                "zipcode": {
37                    errorMessage: "우편번호를 올바르게 입력하세요.",
38                    fnChecking: function(value) {
39                        if (!value || value.trim().length == 0)
40                        {
41                            return true;
42                        }
43
44                        return value.match(/^\d{5}$/);
45                    }
46                },
47                "phoneNumber": {
48                    errorMessage: "전화번호를 올바르게 입력하세요.",
49                    fnChecking: function(value) {
50                        if (!value || value.trim().length == 0)
51                        {
52                            return true;
53                        }
54
55                        return value.match(/^(\d{2,3}-)?\d{3,4}-\d{4}$/);
56                    }
57                },
58                "mobilePhoneNumber": {
59                    errorMessage: "휴대전화번호를 올바르게 입력하세요.",
60                    fnChecking: function(value) {
61                        if (!value || value.trim().length == 0)
62                        {
63                            return true;
64                        }
65
66                        return value.match(/^\d{3}-\d{3,4}-\d{4}$/);
67                    }
68                },
69                "email": {
70                    errorMessage: " ",
71                    fnChecking: function(value) {
72                        if (!value || value.trim().length == 0)
73                        {
74                            return true;
75                        }
76
77                        return value.match(/^[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*\.[a-zA-Z]{2,3}$/i);
78                    }
79                },
80				"check": {
81					errorMessage: "check this",
82                    fnChecking: function(value, element) {
83						if (element.attr("name")) {
84							return (this.form.find("[name='" + element.attr("name") + "']:checked").length > 0);
85						} else {
86							return element.is(":checked");
87						}
88                    }
89				}
90            }
91        };
92
93        var job;
94        var settings;
95
96        if (typeof(arguments[0]) == "string")
97        {
98            job = arguments[0];
99            settings = arguments[1];
100        }
101        else if (typeof(arguments[0]) == "object")
102        {
103            settings = arguments[0];
104        }
105
106		job = job || "init";
107
108        var options = $.extend({}, defaultSettings, settings || {});
109
110        var checker = FormChecker.getManager($(this));
111
112		if ("init" == job) {
113	        checker.setOptions(options);
114			checker.init();
115		} else if ("monitor" == job) {
116			checker.monitor();
117		} else if ("check" == job) {
118			return checker.checkAll();
119		}
120    };
121
122    function FormChecker(form)
123    {
124        this.form = form;
125
126        //var this1 = this;
127        /*this.form.find(".user_input_check").on("blur", function() {
128            this1.isValidInputData($(this));
129        });*/
130    }
131
132    FormChecker.prototype.setOptions = function(options)
133    {
134        this.options = options;
135    };
136
137    FormChecker.prototype.init = function()
138    {
139		var this1 = this;
140		this.fails = {};
141		
142		//$(this.form.find("[data-validation-rule]")).each(function(index, item) {
143		$(this.form.find("input,select,textarea,[data-validation-rule]")).each(function(index, item) {
144			this1.showGuideMessage_($(item));
145			//this1.fails[index] = $(item);
146
147			$(item).on("keyup change click", function() {
148				var result = this1.checkValidation_($(item));
149				//console.log($(item).attr("name"), result);
150				if (!result.success) {
151					//this1.fails[index] = $(item);
152					this1.showFailMessage_($(item), result);
153				} else {
154					//delete this1.fails[index];
155					this1.showGuideMessage_($(item));
156				}
157				
158				if (this1.options.fnMonitor) {
159					var result = this1.checkAll();
160					this1.options.fnMonitor(result.valid, result.invalidList);
161				}
162			});
163		});
164		
165		if (this.options.fnMonitor) {
166			setTimeout(() => {
167				var result = this1.checkAll();
168				this.options.fnMonitor(result.valid, result.invalidList);
169			}, 100);
170		}
171    };
172
173	FormChecker.prototype.checkAll = function()
174    {
175		var this1 = this;
176		var valid = true;
177		var focused = null;
178		var invalidList = [];
179		
180		//$(this.form.find("[data-validation-rule]")).each(function(index, item) {
181		$(this.form.find("input,select,textarea,[data-validation-rule]")).each(function(index, item) {
182			var result = this1.checkValidation_($(item));
183			//console.log("checkAll", $(item), result);
184
185			if (!result.success) {
186				//this1.showFailMessage_($(item), result);
187				valid = false;
188				invalidList.push($(item));
189				
190				if (focused == null) {
191					focused = $(item);
192				}
193				
194			} else {
195				this1.showGuideMessage_($(item));
196			}
197		});
198		
199		if (focused && this1.options.focus) {
200			focused.focus();
201		}
202		//console.log("checkAll", valid);
203		
204		return {valid: valid, invalidList: invalidList};
205    };
206
207    FormChecker.prototype.showGuideMessage_ = function(element)
208    {
209		var field = element.data("validation-guide");
210		if (field) {
211			var message = element.data("validation-guide-message");
212
213			$(field).removeClass("validation_fail");
214			if (message) {
215				$(field).html(message);
216			} else {
217				$(field).html("");
218			}
219		} else if (element.data("validation-success-fn")) {
220			eval(element.data("validation-success-fn"))(element);
221		} else if (this.options.fnSuccessMessage) {
222			this.options.fnSuccessMessage(element);
223		}
224    };
225
226    FormChecker.prototype.showFailMessage_ = function(element, validationResult)
227    {
228		var field = element.data("validation-guide");
229		
230		if (element.data("validation-fail-fn")) {
231			console.log("zzzz");
232			eval(element.data("validation-fail-fn"))(element);
233		} else if (field) {
234			if (validationResult.failMessage) {
235				$(field).addClass("validation_fail");
236				var msg = validationResult.failMessage;
237				if (element.data("validation-guide-message"))
238				{
239					msg += " - " + element.data("validation-guide-message");
240				}
241				
242				$(field).html(msg);
243			}
244		} else if (this.options.fnFailMessage) {
245			this.options.fnFailMessage(element, validationResult);
246		} 
247    };
248
249	FormChecker.prototype.checkValidation_ = function(element)
250    {
251		var result = {success: true};
252		var value = element.val();
253
254        var dataRule = element.data("validation-rule");
255        if (dataRule)
256        {
257            var rules = dataRule.split(" ");
258
259            for (var i in rules)
260            {
261                if (!this.applyValidation(rules[i], value, element))
262                {
263					var rule = this.options.rules[rules[i]];
264	
265					result.success = false;
266					result.failMessage = element.data("validation-fail-message") || rule.errorMessage;
267                    return result;
268                }
269            }
270        }
271
272        var regex = element.data("validation-regex");
273        if (regex)
274        {
275            regex = new RegExp(regex);
276
277            if (!value.match(regex))
278            {
279                var message = element.data("validation-fail-message") || " ";
280
281				result.success = false;
282				result.failMessage = message;
283
284                return result;
285            }
286        }
287
288        var ruleFn = element.data("validation-rule-fn");
289        if (ruleFn)
290        {
291			//console.log("ruleFn", ruleFn);
292            var result1 = eval(ruleFn).call(this, element);
293
294            if (false == result1)
295            {
296				result.success = false;
297				result.failMessage = element.data("validation-fail-message") || " ";
298				//this.showFailMessage_(element, result);
299
300                return result;
301            }
302        }
303
304        return result;
305    };
306
307    FormChecker.prototype.applyValidation = function(ruleName, value, element)
308    {
309        var rule = this.options.rules[ruleName];
310
311        if (rule)
312        {
313            return rule.fnChecking.call(this, value, element);
314        }
315        else
316        {
317            return true;
318        }
319    };
320
321
322    var __formManagers = [];
323
324    FormChecker.getManager = function(elem)
325	{
326		for (var i in __formManagers)
327		{
328			if (elem.is(__formManagers[i].form))
329			{
330				return __formManagers[i];
331			}
332		}
333		
334		return FormChecker.makeManager(elem);
335	};
336
337	FormChecker.makeManager = function(elem)
338	{
339		var manger = new FormChecker(elem);
340		__formManagers.push(manger);
341		
342		return manger;
343	};
344
345
346})(jQuery);
347

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.