[HTML/JavaScript] 绿色版_仿ext验证+仿jquery

taok88 2010-07-30
/*!
 * LT JavaScript Library v0.0.1
 * http://taok8@126.com
 *
 * Copyright (c) 2010 Tao Liu
 * One licensed under the FenDouJiaoYu.
 * http://taok88@126.om
 *
 * Date: 2010-07-30 14:32:21 -0500 (July , 30 Friday 2010)
 * Revision: 100000000
 */

(function() {
	window.onbeforeunload=function (){
			}

	var LiuT = window.LiuT = window.lt = function() {
		return new LiuT.fn();
	};
	LiuT.fn = {
		// 加载对象。可以在body上就可以调用这里的方法。
		ready : function(fn) {
			//
			var l = function() {
				return new LiuT.l();
			}
			l = {
				invoke : function() {
				}
			}
			l.invoke = fn;
			document.attachEvent("onreadystatechange", function() {
						if (document.readyState === "complete") {
							document.detachEvent("onreadystatechange",
									document.attachEvent);
							l.invoke();
						}
					})
		},
		// 检查是否为空
		checkNull : function(id, nullMsg, isNullMsg) {
			if (nullMsg == null) {
				nullMsg = "不能为空";
			}
			if (isNullMsg == null) {
				isNullMsg = "不能为空";
			}
			var object = document.getElementById(id);
			var objectFlg = false;// 控制该对象里是不是有值
			object.value = object.defaultValue;//显示默认值,防止页面刷新时js无效
			if (object.value == "") {
				object.style.color = "#CCCCCC";// 设置没有内容字休颜色
				object.value = nullMsg;
				object.onfocus = f;
				object.onblur = b;
			} else {
				// alert(object.value);
				object.onfocus = f;
				object.onblur = b;
				objectFlg = true;
			}
			function f() {
				//alert(0);
				object.style.color = "";// 去掉内容字休颜色
				if (objectFlg == false) {
					this.value = "";
				} else {
				}
			}
			function b() {
				if (this.value == "") {
					object.style.color = "#CCCCCC";
					object.style.borderColor = "red";// 设置框的颜色
					object.value = isNullMsg;
					objectFlg = false;
				} else {
					objectFlg = true;
					object.style.borderColor = "";// 取消框的颜色
				}
			}
			return objectFlg;
		},
		// 检查email
		checkEmail : function(id) {
			var object = document.getElementById(id);
			var objectFlg = false;// 控制是否正确
			var objectNull = false;// 控制该对象里是不是有值
			object.value = object.defaultValue;
			if (object.value == "") {
				object.style.color = "#CCCCCC";// 设置没有内容字休颜色
				object.value = "请输入邮箱";
				object.onfocus = f;
				object.onblur = b;
			} else {
				object.onfocus = f;
				object.onblur = b;
				objectNull = true;
			}
			function f() {
				object.style.color = "";// 去掉内容字休颜色、
				if (objectFlg == false && objectNull == false) {
					this.value = "";
				}
			}
			function b() {
				if (this.value == "") {
					object.style.color = "#CCCCCC";
					object.style.borderColor = "red";// 设置框的颜色
					object.value = "邮箱不能为空";
					objectNull = false;
					objectFlg = false;
				} else {
					if (object.value
							.match(/^[-+0-9a-z.=_\/]+@([-0-9a-z]+\.)+[a-z]{2,6}$/i)) {
						objectFlg = true;
						objectNull = true;
						object.style.borderColor = "";// 取消框的颜色
					} else {
						object.style.borderColor = "red";// 设置框的颜色
						objectNull = true;
						object.onfocus = f;
						object.onblur = b;
					}
				}
			}
			return objectFlg && objectNull;
		},
		/*
		 * 自定义检查
		 * 
		 */
		selfCheck : function(id, reg, nullMsg, isNullMsg, errorMsg) {
			var object = document.getElementById(id);
			var objectFlg = false;// 控制是否正确
			var objectNull = false;// 控制该对象里是不是有值
			var oPopup = window.createPopup();//显示信息的对象
			object.value = object.defaultValue;
			if (object.value == "") {
				object.style.color = "#CCCCCC";// 设置没有内容字休颜色
				object.value = nullMsg;
				object.onfocus = f;
				object.onblur = b;
			} else {
				object.onfocus = f;
				object.onblur = b;
				objectNull = true;
			}
			function f() {
				object.style.color = "";// 去掉内容字休颜色、
				if (objectFlg == false && objectNull == false) {
					this.value = "";
				}else {
					//document.getElementById("test").value += 1;
				}
			}
			function b() {
				if (this.value == "") {
					//document.getElementById("test").value += 2;
					object.style.color = "#CCCCCC";
					object.style.borderColor = "red";// 设置框的颜色
					object.value = isNullMsg;
					objectNull = false;
					objectFlg = false;
				} else {
					if (reg.exec(object.value)) {
						objectFlg = true;
						objectNull = true;
						object.style.borderColor = "";// 取消框的颜色
						object.onmouseover = null;
					} else {
						objectNull = true;
						object.style.borderColor = "red";// 设置框的颜色
						object.onmouseover = showMsg;
					}
				}
			}
			function showMsg() {
				//document.getElementById("test").value;
				// 显示提示信息
				var oPopupBody = oPopup.document.body;
				oPopupBody.style.backgroundColor = "";
				oPopupBody.style.border = "solid red 3px";
				oPopupBody.style.textAlign  = "center";
				//oPopupBody.style.backgroundColor = "";
				oPopupBody.innerHTML = "<font >"+errorMsg+"</font>";
				oPopup.show(window.event.x, window.event.y, 25*errorMsg.length, 25, document.body);
				object.onmouseout = hidMsg;
			}
			function hidMsg() {
				oPopup.hide();
			}
			return objectFlg;
		}
	}
	lt.checkNull = lt.fn.checkNull;
	lt.checkEmail = lt.fn.checkEmail;
	lt.selfCheck = lt.fn.selfCheck;
	lt.ready = lt.fn.ready;
})();

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
 <script type="text/javascript" src="lt.js" charset="gb2312"></script>
	<script type="text/javascript">
		//开头这里是不是有点像jquery.
		lt.ready(function(){
			lt.checkNull("test","请输入姓名","姓名不能为空!");//这里定义检查空
			lt.checkEmail("test1");//这是检查邮箱
			lt.selfCheck("test2",/\d{3}-\d{8}$|\d{4}-\d{7}$|\d{11,11}$/,"请输入电话号","电话号不能为空!","格式不正确");//自定义检查
		});
	</script>
 </HEAD>

 <BODY>
  
检查不能为空:<input type="text" value="" id="test" >
	<input type="text" value="" id="test1">
	<input type="text" value="" id="test2">
	
	
</body>
</HTML>

 

 

多多交流!!!
Global site tag (gtag.js) - Google Analytics