javascript验证表单输入

//原文地址:http://blog.csdn.net/linwei_1029/article/details/6903245

function isMail(obj,str,allowNull) {
	var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
	if(!isNotNull(obj,str,allowNull)) return false;
	if(!(pattern.test(obj.value))&&!isNull(obj)){
		//document.getElementById('doing').style.visibility='hidden'; 
		alert(str+" 不是合法电子邮件格式!");
		obj.focus();
		return false;
	}
	else return true;
}
//非法字符校验,以英文字母开头其后只能包含英文字母、数字及"_"
function isEN(obj,str,allowNull) {
	var pattern = /^([a-zA-Z])+([a-zA-Z0-9_]*)+$/;
	if(!isNotNull(obj,str,allowNull)) return false;
	if(!(pattern.test(obj.value))&&!isNull(obj)){
		//document.getElementById('doing').style.visibility='hidden';  
		alert(str+" 必须以英文字母开头其后只能包含英文字母、数字及'_'");
		obj.focus();
		return false;
	}
	else return true;
}

function isNotNull (obj,str,allowNull){
  if (isNull(obj) && !allowNull){
 //	document.getElementById('doing').style.visibility='hidden';  
    alert(str+" 不能为空!");
   	obj.focus();
    return false;
  }
  else return true;
}

function isNotNull1 (obj,str,allowNull){
  if ((isNull(obj)||trim(obj.value)=="null") && !allowNull){
 	//document.getElementById('doing').style.visibility='hidden';  
    alert(str+" 不能为空!");
    obj.focus();
    return false;
  }
  else return true;
}

function isNull(obj){
	if(!obj.value || trim(obj.value)=="") return true;
	else return false;
}
//

function isNumber(obj,str,allowNull) {
	var pattern =/^[-,+]{0,1}[0-9]{0,}[.]{0,1}[0-9]{0,}$/;
	if(!isNotNull(obj,str,allowNull)) return false;
	if(!(pattern.test(obj.value))&&!isNull(obj)){
	//	document.getElementById('doing').style.visibility='hidden';  
		alert(str+" 不是数字格式!");
		obj.focus();
		return false;
	}
	else return true;
}

function isInteger(obj,str,allowNull) {
	var pattern = /^-*\d+$/;
	if(!isNotNull(obj,str,allowNull)) return false;
	if(!(pattern.test(obj.value))&&!isNull(obj)){
	//	document.getElementById('doing').style.visibility='hidden';  
		alert(str+" 不是整数格式!");
		obj.focus();
		return false;
	}
	else return true;
}

function isIntegerInfo(obj,str,allowNull) {
	var pattern = /^-*\d+$/;
	if(!isNotNull(obj,str,allowNull)) return false;
	if(!(pattern.test(obj.value))&&!isNull(obj)){
		//document.getElementById('doing').style.visibility='hidden';  
		alert(str);
		obj.focus();
		return false;
	}
	else return true;
}


function isDate(obj,str,allowNull) {
	var pattern = /^[1-9]\d{3}[/|-]((0[1-9])|(1(0|1|2))|([1-9]))[/|-](([0-2][1-9])|([1-2][0-9])|(3(0|1))|([1-9]))$/;
	if(!isNotNull(obj,str,allowNull)) return false;
	if(!(pattern.test(obj.value))&&!isNull(obj)){
		//document.getElementById('doing').style.visibility='hidden';  
		alert(str+" 不是日期格式!");
		obj.focus();
		return false;
	}
	else return true;
}


function verifyPassword(obj1, obj2) {
  if (obj1.value != obj2.value) {
   	 // document.getElementById('doing').style.visibility='hidden';  
	  alert("输入的密码不一致!");
    return false;
  }
  return true;
}

function checkMobile(obj,str,allowNull){
      var pattern=/^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/;
	if(!isNotNull(obj,str,allowNull)) return false;
	if(!(pattern.test(obj.value))&&!isNull(obj)){
	//	document.getElementById('doing').style.visibility='hidden';  	
		alert(str+" 格式不对!");
		obj.focus();
		return false;
	}
	else return true;
}
/**********验证身份证号码的有效性***********/
function isIdCard(obj,str,allowNull){
	var pattern = /^\d{15}(\d{2}[A-Za-z0-9])?$/;
	if(!isNotNull(obj,str,allowNull)) return false;
	if(!(pattern.test(obj.value))&&!isNull(obj)){
	 //   document.getElementById('doing').style.visibility='hidden';  	
		alert(str+" 不是正确的身份证号码!");  
		obj.focus();
		return false;
	}
	else return true;
}

/**********验证手机号码的有效性***********/
function isMobile(obj,str,allowNull){
	var pattern = /^((\(\d{3}\))|(\d{3}\-))?13\d{9}$/;
	if(!isNotNull(obj,str,allowNull)) return false;
	if(!(pattern.test(obj.value))&&!isNull(obj)){
	 //  document.getElementById('doing').style.visibility='hidden';   
		alert(str+" 不是正确的手机号码!");
		obj.focus();
		return false;
	}
	else return true;
}

/**********验证电话号码的有效性***********/
function isTel(obj,str,allowNull){
	var pattern = /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/;
	if(!isNotNull(obj,str,allowNull)) return false;
	if(!(pattern.test(obj.value))&&!isNull(obj)){
	//	document.getElementById('doing').style.visibility='hidden';  	
		alert(str+" 不是正确的电话号码!");
		obj.focus();
		return false;
	}
	else return true;
}

/**********验证email的有效性***********/
function isEmail(obj,str,allowNull){
	var pattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if(!isNotNull(obj,str,allowNull)) return false;
	if(!(pattern.test(obj.value))&&!isNull(obj)){
      // document.getElementById('doing').style.visibility='hidden';   	
		alert(str+" 不是正确的E_mail!");
		obj.focus();
		return false;
	}
	else return true;
}

/**********验证IP地址的有效性***********/
function isIp(obj,str,allowNull){
	if(!isNotNull(obj,str,allowNull)) return false;
	 var check=function(v){try{return (v<=255 && v>=0)}catch(x){return false}};
	 var re=obj.value.split(".");
	 //return (re.length==4)?(check(re[0]) && check(re[1]) && check(re[2]) && check(re[3])):false;
	 	if((re.length==4)?(check(re[0]) && check(re[1]) && check(re[2]) && check(re[3])):false &&!isNull(obj)){
	//	document.getElementById('doing').style.visibility='hidden';  	 	
		alert(str+" 不是正确的IP!");
		obj.focus();
		return false;
	}
	else return true;
}

/**********验证只能为数字或字母***********/
function isNumOrE(obj,str,allowNull){
	var pattern = new RegExp("^[a-zA-Z0-9]+{1}quot;);
	if(!isNotNull(obj,str,allowNull)) return false;
	if(!(pattern.test(obj.value))&&!isNull(obj)){
    //	document.getElementById('doing').style.visibility='hidden';  	
		alert(str+"只能数字或者字母!");
		obj.focus();
		return false;
	}
	else return true;
}

/**********验证邮政编码的有效性***********/
function isZip(obj,str,allowNull){
	var pattern =  /^[1-9]\d{5}$/;
	if(!isNotNull(obj,str,allowNull)) return false;
	if(!(pattern.test(obj.value))&&!isNull(obj)){
	//	document.getElementById('doing').style.visibility='hidden';  	
		alert(str+" 不是正确的邮政编码!");
		obj.focus();
		return false;
	}
	else return true;
}
/**********去除左右空格***********/
function trim(str){
	return str.replace(/(^\s*)|(\s*$)/g, "");
}
/*
function isNumber(obj) {
	var pattern =/^[-,+]{0,1}[0-9]{0,}[.]{0,1}[0-9]{0,}$/;
	var oldValue = obj.value;
	if(!(pattern.test(obj.value))){
		document.getElementById('doing').style.visibility='hidden';  
		alert("不是数字格式!");
		obj.value = oldValue;
		obj.focus();
		return false;
	}
	else return true;
}*/
function strToDate(str)
{
  var arys= new Array();
  arys=str.split('-');
  var newDate=new Date(arys[0],arys[1],arys[2]); 
  return newDate;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值