隨筆-13  評論-12  文章-3  trackbacks-0
           

          1數(shù)字

           

           function testisNum(object)
                                 {
                    
                                  
          var s =document.getElementById(object.id).value;
                        
                                      
          if(s!="")
                                      {
                                           
          if(isNaN(s))
                                          {
                                           alert("
          請輸入數(shù)字");
                                           object.value="";
                                           object.focus();
                                          }
                                      }
                                  }

           

          2、電話號碼,傳真

           

           //校驗(yàn)普通電話、傳真號碼:可以“+”開頭,除數(shù)字外,可含有“-”
                      function isTel(object)
                      {
                      
          //國家代碼(23)-區(qū)號(23)-電話號碼(78)-分機(jī)號(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)-區(qū)號(23)-電話號碼(78)-分機(jī)號(3)"');
                                object.value="";
                                object.focus();
                               }
                           }
                      }

           

          3、郵箱

           

           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、手機(jī)號碼

           

           //校驗(yàn)手機(jī)號碼:必須以數(shù)字開頭,除數(shù)字外,可含有“-”
                       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('
          請輸入正確的手機(jī)號碼');
                                 object.value="";
                                 object.focus();
                              }
                          }
                      }

           

           

           //校驗(yàn)日期
                      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、郵編

           

           //校驗(yàn)(國內(nèi))郵政編碼
                      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、日期

           

           //校驗(yàn)日期
                      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();
                               }
                           }         
                      }

           

          posted on 2010-11-17 21:44 zeroline 閱讀(5836) 評論(2)  編輯  收藏 所屬分類: JavaScript

          評論:
          # re: 常用的js驗(yàn)證數(shù)字,電話號碼,傳真,郵箱,手機(jī)號碼,郵編,日期 2012-10-25 17:32 | sds
          sdsds  回復(fù)  更多評論
            
          # re: 常用的js驗(yàn)證數(shù)字,電話號碼,傳真,郵箱,手機(jī)號碼,郵編,日期 2015-08-19 23:46 | 把你們嗎
          進(jìn)門口集合嗎  回復(fù)  更多評論
            
          主站蜘蛛池模板: 台山市| 雷山县| 开原市| 贵州省| 三亚市| 红安县| 庆安县| 阜城县| 庆城县| 平泉县| 宁化县| 宁南县| 新营市| 鄄城县| 安乡县| 杂多县| 太谷县| 陵川县| 循化| 渑池县| 衡南县| 古蔺县| 辰溪县| 安庆市| 孝义市| 正安县| 岳西县| 科尔| 新宁县| 滁州市| 山阳县| 扶沟县| 专栏| 濮阳县| 那曲县| 台州市| 弋阳县| 桑植县| 乐都县| 太保市| 高台县|