`
dengyll
  • 浏览: 90501 次
社区版块
存档分类
最新评论

常用的js验证代码

 
阅读更多

常用的js验证代码

//是否为空校验
function isEmpty(s) {
	var lll=trim(s);
	if( lll == null || lll.length == 0 )
		return true;
	else
		return false;
}
//删除字符串左边的空格
function ltrim(str) { 
	if(str.length==0)
		return(str);
	else {
		var idx=0;
		while(str.charAt(idx).search(/\s/)==0)
			idx++;
		return(str.substr(idx));
	}
}

//删除字符串右边的空格
function rtrim(str) { 
	if(str.length==0)
		return(str);
	else {
		var idx=str.length-1;
		while(str.charAt(idx).search(/\s/)==0)
			idx--;
		return(str.substring(0,idx+1));
	}
}

//删除字符串左右两边的空格
function trim(str) { 
	return(rtrim(ltrim(str)));
}

/*日期相比较*/
function compareDate(date1, date2) {
	if (trim(date1) == trim(date2))  	
		return 0;
	if (trim(date1) > trim(date2))  	
		return 1;
	if (trim(date1) < trim(date2))  	
		return -1;
}

//校验是否是Email
function isEmail(eml) {
	if(trim(eml)!='') {
	  var re=new RegExp("@[\\w]+(\\.[\\w]+)+{1}quot;);
	  return(re.test(eml));
	}
	else
	  return(true);
}

//是否是电话号
function isTel(tel) {
	var charcode;
	for (var i=0; i<tel.length; i++)	
	{
		charcode = tel.charCodeAt(i);
		if (charcode < 48 && charcode != 45 || charcode > 57)	
			return false;
	}
	return true;
}

//校验是否是实数
function isnumber(num) {
	var re=new RegExp("^-?[\\d]*\\.?[\\d]*{1}quot;);
	if(re.test(num))
		return(!isNaN(parseFloat(num)));
	else
		return(false);
}

//校验是否是整数
function isinteger(num)	{
	var re=new RegExp("^-?[\\d]*{1}quot;);
	if(re.test(num))
		return(!isNaN(parseInt(num)));
	else
		return(false);
}
//校验是否是数字和字母组合,并且是4-6位
function isNumOrLetters(str){
var re = new RegExp(/^[a-zA-Z0-9]{4,6}$/);
if(re.test(trim(str)))
return(true);
else
return(false);
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics