﻿<!--

// 待验证控件集
var Page_Validators = new Array();

// 页面验证-----------------------------------------------//
function ValidatorOnSubmit(frmObj) {	
	for(var i=0; i<Page_Validators.length; i++) {
		try
		{
		var obj = $(Page_Validators[i][0]);
		if( !obj ) continue;
		
		if( obj.getAttribute("isrequired")==null ) continue;
	
		if( (obj.getAttribute("isrequired")=="必填") && (obj.value.trim()=="") ) {
			// 如果控件为必填项，且没有输入，直接输出false
			alert(obj.getAttribute("errormessage"));
			obj.focus();
			return false;
		}
		
		if( (obj.getAttribute("isrequired")!="必填") && (obj.value.trim()=="") ) {
			// 如果控件不是必填项，且没有输入，无须判断，直接转到下一个控件判断
			continue;
		}
			
		// 判断控件输入是否合法
		if( !ValidateValue(obj, obj.getAttribute("validate"), obj.getAttribute("errormessage"), true) ) return false;		
		}
		catch(e)
		{
		alert(obj.tagName);
		return false;
		}			
	}
	
	return true;
	
}
// 页面验证-----------------------------------------------//

// 初始化页面验证控件-------------------------------------//
function InitValidator() {
	if( Page_Validators==null) return;

	for(var i=0; i<Page_Validators.length; i++) {
		try {
			var obj = $(Page_Validators[i][0]);
			obj.setAttribute("isrequired", Page_Validators[i][1]);
			obj.setAttribute("validate", Page_Validators[i][2]);
			obj.setAttribute("errormessage", Page_Validators[i][3]);
		}
		catch(e) {
			continue;
		}
	}
}
// 初始化页面验证控件-------------------------------------//

function ValidateValue(obj, validater, message, onfocus) {

	obj.value = obj.value.trim();

	// 取得控件输入判断正则表达式
	var validate = null;
	switch(validater)
	{
		case "日期":
			validate = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";
			break;
		case "纯数字":
			validate = "^\\d+$";
			break;
		case "数值":
			validate = "^\\d+\\.?\\d+$";
			break;
		case "固定电话":
			validate = "(^[0-9]{3,4}\-[0-9]{3,8}(\-[0-9]{1,4})?$)|(^[0-9]{3,8}(\-[0-9]{1,4})?$)|(^\([0-9]{3,4}\)[0-9]{3,8}(\-[0-9]{1,4})?$)|(^[0-9]{3,4}\-[0-9]{3,8}\-[0-9]{1,4}$)";
			break;
		case "手机号码":
			validate = "^0{0,1}1(3[0-9]{9}|5[0,3,6,8,9][0-9]{8})$";
			break;
		case "电子邮箱":
			validate = "^[_a-zA-Z0-9\\-]+(\\.[_a-zA-Z0-9\\-]*)*@[a-zA-Z0-9\\-]+([\\.][a-zA-Z0-9\\-]+)+$";
			break;
		case "网页地址":
			validate = "^(http|https)\\://([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&%\\$\\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\\:[0-9]+)*(/($|[a-zA-Z0-9\\.\\,\\?\\'\\\\\\+&%\\$#\\=~_\\-]+))*$";
			break;		
		default:
			validate = validater;
			break;
	}
	
	var patn = new RegExp(validate, "ig");
			
	if( !obj.value.match(patn) ) {
		alert(message);
		if( onfocus )
		{
			obj.focus();
			if( obj.type=="text" )
				obj.select();
		}
		return false;
	}
	
	return true;
	
}

-->