常用JS验证

常用的js验证数字,电话真,箱,手机号邮编,日期

Posted on 2009-08-25 14:08 pcajax 阅读(1494) 评论(0) 编辑 收藏

 

1数字

 

Code
 function testisNum(object)
                       {
          
                        
var s =document.getElementById(object.id).value;
              
                            
if(s!="")
                            {
                                 
if(isNaN(s))
                                {
                                 alert("
请输数字");
                                 object.value="";
                                 object.focus();
                                }
                            }
                        }

 

2电话

 

Code
 //
普通电话真号:可以“+”开头,除数字外,可含有“-”
            function isTel(object)
            {
            
//国家代(23)-区号(23)-电话(78)-分机号(3)"

             
var s =document.getElementById(object.id).value; 
             
var pattern =/^(([0/+]/d{2,3}-)?(0/d{2,3})-)(/d{7,8})(-(/d{3,}))?$/;
             
//var pattern =/(^[0-9]{3,4}/-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^/([0-9]{3,4}/)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/; 
                 if(s!="")
                 {
                     
if(!pattern.exec(s))
                     {
                      alert('
请输入正确的电话:电话格式国家代(23)-区号(23)-电话(78)-分机号(3)"');
                      object.value="";
                      object.focus();
                     }
                 }
            }

 

3

 

Code
 function Check(object)
          { 
        
var s =document.getElementById(object.id).value; 
             
var pattern =/^[a-zA-Z0-9_/-]{1,}@[a-zA-Z0-9_/-]{1,}/.[a-zA-Z0-9_/-.]{1,}$/;
                 
if(s!="")
                 {
                     
if(!pattern.exec(s))
                     {
                      alert('
请输入正确的箱地址');
                      object.value="";
                      object.focus();
                     }
                 }
                
        }

 

4、手机号

 

Code
 //
手机号:必数字开头,除数字外,可含有“-”
             function isMobile(object)
            {
            
var s =document.getElementById(object.id).value; 
            
var reg0 = /^13/d{5,9}$/;
            
var reg1 = /^153/d{4,8}$/;
            
var reg2 = /^159/d{4,8}$/;
            
var reg3 = /^0/d{10,11}$/;
            
var my = false;
            
if (reg0.test(s))my=true;
            
if (reg1.test(s))my=true;
            
if (reg2.test(s))my=true;
            
if (reg3.test(s))my=true;
                
if(s!="")
                {
                    
if (!my)
                    {
                       alert('
请输入正确的手机号');
                       object.value="";
                       object.focus();
                    }
                }
            }

 

 

Code
 //
日期
            function isdate(object)
            {
             
var s =document.getElementById(object.id).value; 
             
var pattern =/^((/d{2}(([02468][048])|([13579][26]))[/-///s]?((((0?[13578])|(1[02]))[/-///s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[/-///s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[/-///s]?((0?[1-9])|([1-2][0-9])))))|(/d{2}(([02468][1235679])|([13579][01345789]))[/-///s]?((((0?[13578])|(1[02]))[/-///s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[/-///s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[/-///s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(/s(((0?[0-9])|([1-2][0-3]))/:([0-5]?[0-9])((/s)|(/:([0-5]?[0-9])))))?$/;
                 
if(s!="")
                 {
                     
if(!pattern.exec(s))
                     {
                      alert('
请输入正确的日期');
                      object.value="";
                      object.focus();
                     }
                 }         
            }

5邮编

 

Code
 //
(国内)编码
            function isPostalCode(object)
            {
             
var s =document.getElementById(object.id).value; 
             
var pattern =/^[0-9]{6}$/;
                 
if(s!="")
                 {
                     
if(!pattern.exec(s))
                     {
                      alert('
请输入正确的编码');
                      object.value="";
                      object.focus();
                     }
                 }
            }

 

6、日期

 

Code
 //
日期
            function isdate(object)
            {
             
var s =document.getElementById(object.id).value; 
             
var pattern =/^((/d{2}(([02468][048])|([13579][26]))[/-///s]?((((0?[13578])|(1[02]))[/-///s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[/-///s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[/-///s]?((0?[1-9])|([1-2][0-9])))))|(/d{2}(([02468][1235679])|([13579][01345789]))[/-///s]?((((0?[13578])|(1[02]))[/-///s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[/-///s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[/-///s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(/s(((0?[0-9])|([1-2][0-3]))/:([0-5]?[0-9])((/s)|(/:([0-5]?[0-9])))))?$/;
                 
if(s!="")
                 {
                     
if(!pattern.exec(s))
                     {
                      alert('
请输入正确的日期');
                      object.value="";
                      object.focus();
                     }
                 }         
            }

 

你可能感兴趣的:(object,手机,电话)