var __NOW_UID = new Date().getTime(); /*------------------------------------------*/ var __MESSAGE_REQUIRED = '"{0}" 不能为空,必须填写!'; var __MESSAGE_INVALID_DATE = '"{0}" 日期格式不正确!'; var __MESSAGE_DATE_BEFORE = '"{0}" 不能早于'; var __MESSAGE_DATE_AFTER = '"{0}" 不能晚于'; var __MESSAGE_INVALID_NUM = '"{0}" 数字格式不正确!'; var __MESSAGE_GT = '"{0}" 必须大于'; var __MESSAGE_LT = '"{0}" 必须小于'; var __MESSAGE_GE = '"{0}" 不能小于'; var __MESSAGE_LE = '"{0}" 不能大于'; function uuid() { return "U" + __NOW_UID ++; } $(document).ready(function(){ var actionMessage = new Array(); var actionError = new Array(); $(".actionMessage span").each(function(){ actionMessage[actionMessage.length] = ($(this).html()); }); if (actionMessage.length > 0) { var div = $("
"); var message = ""; for (var i = 0; i < actionMessage.length; i ++) { message += actionMessage[i]; if (i < actionMessage.length - 1) message += "
"; } div.html(message); $(".actionMessage span").parents("ul:first").after(div).remove(); } $(".errorMessage span").each(function(){ actionError[actionError.length] = ($(this).html()); }); if (actionError.length > 0) { var div = $("
"); var message = ""; for (var i = 0; i < actionError.length; i ++) { message += actionError[i]; if (i < actionError.length - 1) message += "
"; } div.html(message); $(".errorMessage span").parents("ul:first").after(div).remove(); } $(document.body).validatable(); $(document).keydown(function(evt) { var target = evt.target.tagName; if (evt.which == 13 && target == 'INPUT' || target == 'SELECT') { if (evt.target.type == 'button') { if (typeof(evt.target.onclick) == 'function') { evt.target.onclick.call(evt.target); } else { $(evt.target).trigger('click'); } } if($.browser.msie) evt.keyCode = 9; } }); }); jQuery.fn.extend({ validatable : function() { $("input.datepicker", $(this)).each(function(){ var input = this; var pattern = typeof(input.format) != 'undefined' ? input.pattern : 'yyyy-MM-dd'; var offset = $(this).offset(); $(this).attr('pattern', pattern).css("imeMode", "disabled").click(function(e) { var pos = $(this).offset(); if (pos.left + $(this).width() - e.clientX < 18) { popCalendar(this, pattern, false); } }); }); $("input.numberbox", $(this)).keypress(function(evt){ var key = evt.which; if (key != 45 && (key != 46 || $(this).hasClass("integer")) && key != 8 && key != 37 && key != 39 && (key < 48 || key > 57)) { evt.preventDefault(); } else if (key == 46 && this.value.indexOf(".") != -1) { evt.preventDefault(); } }).keyup(function(evt) { var ctrl = evt.ctrlKey; if (ctrl && evt.keyCode == 86) { if (isNaN(this.value)) this.value = ''; } }); $("input.required, input.datepicker, input.numberbox, select.required, textarea.required", $(this)).filter(":visible").each(function(){ $(this).blur(function(){ if (this.getAttribute("hcksid") == null) { this.setAttribute("hcksid", uuid()); } var message = null; if ($(this).hasClass('required') && $.trim(this.value) == '') { message = __MESSAGE_REQUIRED; } else if ($(this).hasClass('datepicker')) { if (!validateDate(this.value, $(this).attr('pattern'))) { message = __MESSAGE_INVALID_DATE; } else { var matchResult = this.className.match(/(before|after)(#\w+)?/); if (matchResult != null && matchResult.length > 1) { var otherDate = __TODAY; if (matchResult.length > 2 && matchResult[2]) { var otherDp = document.getElementById(matchResult[2].substr(1)); if (otherDp != null && otherDp.value.length > 0) { var otherDateArr = extractDate(otherDp.value, $(otherDp).attr('pattern')); otherDate = new Date(otherDateArr[0], otherDateArr[1], otherDateArr[2]); } } var thisDateArr = extractDate(this.value, $(this).attr('pattern')); var thisDate = new Date(thisDateArr[0], thisDateArr[1], thisDateArr[2]); if (thisDate.getTime() > otherDate.getTime() && matchResult[1] == 'before') { message = __MESSAGE_DATE_AFTER + otherDate.format(); } else if (thisDate.getTime() < otherDate.getTime() && matchResult[1] == 'after') { message = __MESSAGE_DATE_BEFORE + otherDate.format(); } } } } else if (this.value.length > 0 && $(this).hasClass('numberbox')) { if (isNaN(this.value) || this.value.match(/^\s+|\s+$/) != null) { message = __MESSAGE_INVALID_NUM; } else { var regx = /(?:^|\s)(gt|lt|ge|le)(-?\d+(?:\.\d+)?)/g; var mms = this.className.match(regx); if (mms != null) { for (var i = 0; i < mms.length; i++) { var r = /(?:^|\s)(gt|lt|ge|le)(-?\d+(?:\.\d+)?)/g; var matches = r.exec(mms[i]); if (matches != null && matches.length > 1) { var num = parseFloat(matches[2]); var thisnum = parseFloat(this.value); if (matches[1] == "gt" && (isNaN(thisnum) || thisnum <= num)) { message = __MESSAGE_GT + num; } else if (matches[1] == "lt" && (isNaN(thisnum) || thisnum >= num)) { message = __MESSAGE_LT + num; } else if (matches[1] == "ge" && (isNaN(thisnum) || thisnum < num)) { message = __MESSAGE_GE + num; } else if (matches[1] == "le" && (isNaN(thisnum) || thisnum > num)) { message = __MESSAGE_LE + num; } if (message != null) break; } } } } } if (message != null) { $(this).addClass("x-form-invalid"); var icons = $("#v" + this.getAttribute("hcksid")); message = message.replace(/"\{0\}"/g, this.title); if (icons.length == 0) { var offset = $(this).offset(); var prevSib = $(this).next("img"); var invalidIcon = $("").attr("src", __BLANK_IMAGE).addClass('x-form-invalid-icon') .attr("title", message); if (prevSib.length == 0) { prevSib = $(this); } else { invalidIcon.css("left", "-16px"); } prevSib.after(invalidIcon); } else { icons.attr("title", message); } } else { $(this).removeClass("x-form-invalid"); $("#v" + this.getAttribute("hcksid")).remove(); } }); }); $("form", $(this)).submit(function() { if ($(this).attr("validate") == false) { return true; } var r = $(this).validate(); if (r == false) { alert("表单填写有误,请检查带有红色叹号标记的表单"); // var errMsgs = []; // $(":text.x-form-invalid, select.x-form-invalid", this).each( function() { // var erricon = $("#v" + this.getAttribute("hcksid")); // if (erricon.length > 0) errMsgs.push(erricon.attr("title")); // }); // if (errMsgs.length > 0) { // var errPanel = $("#HCKS_VALIDATE_ERROR_PANEL"); // if (errPanel.length == 0) { // errPanel = $("
"); // errPanel.append("
表单填写有误,请检查带有红色叹号标记的表单
"); // errPanel.append("
"); // var btnDiv = $("
"); // var errok = $("").click(function() { // $("div.errreportbody", errPanel).html(""); // errPanel.unblockUI(); // }); // errPanel.append(btnDiv.append(errok)); // $(document.body).append(errPanel); // } // var msgs = ""; // for (var i = 0; i < errMsgs.length; i++) { // if (i > 0) msgs += "
"; // msgs += (i + 1) + "、" + errMsgs[i]; // } // $("div.errreportbody", errPanel).html(msgs); // errPanel.blockUI(); // } } if (r) { $("input[type=button]", this).each(function() { if (this.className.indexOf('enable') == -1) { this.disabled = true; } }); } return r; }); return $(this); }, validate : function() { var valid = true; if ($(this).attr("validate") != "false") { $("input.required, input.datepicker, input.numberbox, select.required, textarea.required", this).filter( function() { return $(this).is(":visible") && $(this).parents(":hidden").length == 0; } ).each( function() { $(this).blur(); if (valid) { valid = !$(this).hasClass("x-form-invalid"); } }); } return valid; }, revertValidation : function() { $(":text.x-form-invalid, select.x-form-invalid", this).each( function() { $(this).removeClass("x-form-invalid"); $("#v" + this.getAttribute("hcksid")).remove(); }); return $(this); } });