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(){ $(document.body).validatable(); }); 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); alert(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("表单填写有误,请检查带有红色叹号标记的表单"); } 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); } });