/**
 * 验证是否是一个合法的数字
 */
function onlyNum()
{
 if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false
 //考虑小键盘上的数字键

}

function isNumber(value){
  if(isNaN(value) || value == ""){
    return false;
  }
  return true;
}
function isNumber1(value){
  if(isNaN(value)&& value != ""){
    return false;
  }
  return true;
}

/**
 * 验证字符串是否为空,忽略空格。
 */
function isEmptyIgnoreBlank(str){
  if(str.replace(/ /g,"") == ""){
    return true;
  }
  return false;
}

/**
 * 验证字符串是否为空,不忽略空格。
 */
function isEmpty(str){
  if(str == "" || str == null){
    return true;
  }
  return false;
}

function maxlengthStr(value,len){
  if(value.replace(/[^\x00-\xff]/g,"**").length > len){
    return false;
  }
  return true;
}

function minlengthStr(value,len){
  if(value.replace(/[^\x00-\xff]/g,"**").length < len){
    return false;
  }
  return true;
}

function length(value){
  return value.replace(/[^\x00-\xff]/g,"**").length;
}

function isInteger(value){
  return  isNumber(value)&&(value.indexOf(".")==-1)
}

/**
 * 验证页面中元素是类型
 */
function getType(type){
  if(type=='string'){
  return null;
  }else if(type=='float'){
  return 0;
  }else if(type=='int'){
  return 1;
  }else if(type=='date'){
  return 2;
  }else if(type=='datetime'){
  return 3;
  }
}
/**
 * 验证页面中元素是否可以为空,类型是什么,并且限制其长度。
 */

function isNullTypeMaxMinLength(flag,type,maxlength,minlength,strObj,strLabel)
{
    var ch=eval("document.forms[0]."+strObj);
	if(flag&&isEmptyIgnoreBlank(ch.value))
	return true;
	if(type=='checkbox'){
		var checked = false;
		var checkboxes = document.getElementsByName(strObj);
		for(var i = 0;i<checkboxes.length;i++){
			if(checkboxes[i].checked){
				checked=true;
			}
		}
		if(!checked){
			alert(strLabel+"没有选中");
			return false;
		}
	}
    if(!flag&&isEmptyIgnoreBlank(ch.value))
    {
  	    alert(strLabel+"不能为空");
  	    ch.focus();
  	    return false;
  	}
  	if(!isEmptyIgnoreBlank(type+'')&&type==0&&!isNumber(ch.value))
  	{
  		alert(strLabel+"必须为数字");
  	    ch.focus();
  	    return false;
  	}
  	if(!isEmptyIgnoreBlank(type+'')&&type==1&&!isInteger(ch.value))
  	{
  		alert(strLabel+"必须为整数");
  	    ch.focus();
  	    return false;
  	}
  	if(!isEmptyIgnoreBlank(maxlength+'')&&length(ch.value)!=maxlength&&maxlength==minlength)
  	{
  		alert(strLabel+"的长度必须为"+maxlength+"个字符");
  	    ch.focus();
  	    return false;
    }
    if(!isEmptyIgnoreBlank(maxlength+'')&&!maxlengthStr(ch.value,maxlength))
    {
  		if(type==null) alert(strLabel+"长度不应大于"+maxlength/2+"个汉字");
  		else alert(strLabel+"长度不应大于"+maxlength+"个数字");
  	    ch.focus();
  	    return false;
  	}
  	if(!isEmptyIgnoreBlank(minlength+'')&&!minlengthStr(ch.value,minlength))
  	{
  		alert(strLabel+"最小长度为"+minlength+"个字符");
  	    ch.focus();
     	return false;
    }
    return  true;
}

