[转]javasript的几个函数

本文介绍了几种使用JavaScript函数进行数据验证的方法,包括检查输入是否为汉字、数字及电子邮件地址的有效性。通过正则表达式和字符编码判断提供了一种简单而有效的验证方式。
判断是否为汉字的函数
None.giffunction   checkchar(str)   
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{   
InBlock.gif  var   temp
=str.length;   
InBlock.gif  
for   (i=0;i<temp;i++)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
if   ((str.charCodeAt(i)>57)   &&   (str.charCodeAt(i)<65))   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
return   false;   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
if   ((str.charCodeAt(i)>90)&&(str.charCodeAt(i)<97))   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
return   false;   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
if   (str.charCodeAt(i)<48)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
return   false;   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
if   ((str.charCodeAt(i)>122))   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
return   false;   
ExpandedSubBlockEnd.gif  }
   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
return   true;   
ExpandedBlockEnd.gif  }
 
判断是否为数字的函数
None.gif  function   checknum(str)   
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{   
InBlock.gif  var   temp
=str.length;   
InBlock.gif  
for   (i=0;i<temp;i++)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
if   (str.charCodeAt(i)>57)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
return   false;   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
if   (str.charCodeAt(i)<48)   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
return   false;   
ExpandedSubBlockEnd.gif  }
   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
return   true;   
ExpandedBlockEnd.gif  }
 
ExpandedBlockStart.gifContractedBlock.gif                        function checknum(obj)dot.gif
InBlock.gif                        
InBlock.gif                        var pattern
=/^\d+$/ ; 
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
if(!pattern.exec(obj.value))dot.gif{
InBlock.gif                        alert(
"请输入数字"); 
ExpandedSubBlockEnd.gif                      
return false;}

ExpandedBlockEnd.gif}

判断是否为email的函数
None.gif  function   emailcheck(email)   
ExpandedBlockStart.gifContractedBlock.gif  
dot.gif{     
ExpandedSubBlockStart.gifContractedSubBlock.gif  var   yufan   
=   /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]dot.gif{2,3}$/;     
InBlock.gif  
if(yufan.test(email))   
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{   
InBlock.gif  
return   true;   
ExpandedSubBlockEnd.gif  }
   
InBlock.gif  
return   false;     
ExpandedBlockEnd.gif  }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值