[转帖]Javascript字符验证代码

   
<script language="javascript"> 
function check() 

var ErrorInfo="",email="",mobi="",phone="",openmoney="",flags=0
email
=document.form1.email.value; 
mobi
=document.form1.mobi.value; 
phone
=document.form1.phone.value; 
//alert(email); 
if (email.lastIndexOf("@")== -1 || email.lastIndexOf(".")== -1

ErrorInfo
=ErrorInfo+"邮件地址不正确!";flags=1

if (isNaN(document.form1.mobi.value)) 

ErrorInfo
=ErrorInfo+"移动电话带有非数字字符!\n";flags=2

if (isNaN(document.form1.phone.value)) 

ErrorInfo
=ErrorInfo+"固定电话带有非数字字符!\n";flags=3

if (isNaN(document.form1.openmoney.value)) 

ErrorInfo
=ErrorInfo+"开户金额带有非数字字符!\n";flags=4

if (email.lastIndexOf("sina"!= -1

ErrorInfo
=ErrorInfo+"不支持sina邮箱!";flags=5

if (flags) { alert(ErrorInfo); return false;} 
return true

</script> 

//函数名:chkdate 
//
功能介绍:检查是否为日期 
//
参数说明:要检查的字符串 
//
返回值:0:不是日期 1:是日期 
function chkdate(datestr) 

var lthdatestr 
if (datestr != ""
lthdatestr
= datestr.length ; 
else 
lthdatestr
=0

var tmpy=""
var tmpm=""
var tmpd=""
//var datestr; 
var status; 
status
=0
if ( lthdatestr== 0
return 0 


for (i=0;i<lthdatestr;i++
if (datestr.charAt(i)== '-') 

status
++

if (status>2

//alert("Invalid format of date!"); 
return 0

if ((status==0&& (datestr.charAt(i)!='-')) 

tmpy
=tmpy+datestr.charAt(i) 

if ((status==1&& (datestr.charAt(i)!='-')) 

tmpm
=tmpm+datestr.charAt(i) 

if ((status==2&& (datestr.charAt(i)!='-')) 

tmpd
=tmpd+datestr.charAt(i) 



year
=new String (tmpy); 
month
=new String (tmpm); 
day
=new String (tmpd) 
//tempdate= new String (year+month+day); 
//
alert(tempdate); 
if ((tmpy.length!=4|| (tmpm.length>2|| (tmpd.length>2)) 

//alert("Invalid format of date!"); 
return 0

if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) ) 

//alert ("Invalid month or day!"); 
return 0

if (!((year % 4)==0&& (month==2&& (day==29)) 

//alert ("This is not a leap year!"); 
return 0

if ((month<=7&& ((month % 2)==0&& (day>=31)) 

//alert ("This month is a small month!"); 
return 0


if ((month>=8&& ((month % 2)==1&& (day>=31)) 

//alert ("This month is a small month!"); 
return 0

if ((month==2&& (day==30)) 

//alert("The Febryary never has this day!"); 
return 0


return 1


//函数名:fucCheckNUM 
//
功能介绍:检查是否为数字 
//
参数说明:要检查的数字 
//
返回值:1为是数字,0为不是数字 
function fucCheckNUM(NUM) 

var i,j,strTemp; 
strTemp
="0123456789"
if ( NUM.length== 0
return 0 
for (i=0;i<NUM.length;i++

j
=strTemp.indexOf(NUM.charAt(i)); 
if (j==-1

//说明有字符不是数字 
return 0


//说明是数字 
return 1


//函数名:chknum 
//
功能介绍:检查是否为数字 
//
参数说明:要检查的数字 
//
返回值:1为是数字,0为不是数字 
function chknum(NUM) 

var i,j,strTemp; 
strTemp
="-.0123456789"
if ( NUM.length== 0
return 0 
for (i=0;i<NUM.length;i++

j
=strTemp.indexOf(NUM.charAt(i)); 
if (j==-1

//说明有字符不是数字 
return 0


//说明是数字 
return 1




//函数名:chkdate 
//
功能介绍:检查是否为格式为YYYYMMDD日期 
//
参数说明:要检查的字符串 
//
返回值:0:不是日期 1:是日期 
function chkdate(datestr) 

var lthdatestr 
if (datestr != ""
lthdatestr
= datestr.length ; 
else 
lthdatestr
=0

var tmpy=""
var tmpm=""
var tmpd=""
//var datestr; 
var status; 
status
=0
if(lthdatestr<8) {return 0;}//长度不等于8 
if(lthdatestr>8) {return 0;}//长度不等于8 
//
含有非数字 
for(i=0;i<lthdatestr;i++

single_b
=datestr.charAt(i);//取单个数字 
var num=new Number(single_b); 
if(isNaN(num)==true) {return 0;} 


tmpy
=datestr.substring(0,4); 
tmpm
=datestr.substring(4,6); 
tmpd
=datestr.substring(6); 

year
=new Number(tmpy); 
month
=new Number(tmpm); 
day
=new Number(tmpd) 
//tempdate= new String (year+month+day); 
//
alert(tempdate); 
if ((tmpy.length!=4|| (tmpm.length>2|| (tmpd.length>2)) 

return 0

if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) ) 

return 0

if (!((year % 4)==0&& (month==2&& (day==29)) 

return 0

if ((month<=7&& ((month % 2)==0&& (day>=31)) 

return 0


if ((month>=8&& ((month % 2)==1&& (day>=31)) 

return 0

if ((month==2&& (day==30)) 

return 0


return 1
posted @ 2007-04-05 10:39  涂文瀚  阅读(684)  评论(1编辑  收藏  举报
foot