常用JS驗證,數字、手機、電話、傳真、郵編、日期

1、數字

Js代碼
  1. function testisNum(object)
  2. {

  3. var s =document.getElementById(object.id).value;

  4. if(s!="")
  5. {
  6. if(isNaN(s))
  7. {
  8. alert("請輸入數字");
  9. object.value="";
  10. object.focus();
  11. }
  12. }
  13. }
function testisNum(object)
                       {
          
                        var s =document.getElementById(object.id).value;
              
                            if(s!="")
                            {
                                 if(isNaN(s))
                                {
                                 alert("請輸入數字");
                                 object.value="";
                                 object.focus();
                                }
                            }
                        }



2、電話號碼,傳真

Js代碼
  1. //校驗普通電話、傳真號碼:可以“+”開頭,除數字外,可含有“-”
  2. function isTel(object)
  3. {
  4. //國家代碼(2到3位)-區號(2到3位)-電話號碼(7到8位)-分機號(3位)"

  5. var s =document.getElementById(object.id).value;
  6. var pattern =/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
  7. //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}$)/;
  8. if(s!="")
  9. {
  10. if(!pattern.exec(s))
  11. {
  12. alert('請輸入正確的電話號碼:電話號碼格式爲國家代碼(2到3位)-區號(2到3位)-電話號碼(7到8位)-分機號(3位)"');
  13. object.value="";
  14. object.focus();
  15. }
  16. }
  17. }
//校驗普通電話、傳真號碼:可以“+”開頭,除數字外,可含有“-”
            function isTel(object)
            {
            //國家代碼(2到3位)-區號(2到3位)-電話號碼(7到8位)-分機號(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('請輸入正確的電話號碼:電話號碼格式爲國家代碼(2到3位)-區號(2到3位)-電話號碼(7到8位)-分機號(3位)"');
                      object.value="";
                      object.focus();
                     }
                 }
            }



3、郵箱

Js代碼
  1. function Check(object)
  2. {
  3. var s =document.getElementById(object.id).value;
  4. var pattern =/^[a-zA-Z0-9_\-]{1,}@[a-zA-Z0-9_\-]{1,}\.[a-zA-Z0-9_\-.]{1,}$/;
  5. if(s!="")
  6. {
  7. if(!pattern.exec(s))
  8. {
  9. alert('請輸入正確的郵箱地址');
  10. object.value="";
  11. object.focus();
  12. }
  13. }

  14. }
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、手機號碼

Js代碼
  1. //校驗手機號碼:必須以數字開頭,除數字外,可含有“-”
  2. function isMobile(object)
  3. {
  4. var s =document.getElementById(object.id).value;
  5. var reg0 = /^13\d{5,9}$/;
  6. var reg1 = /^153\d{4,8}$/;
  7. var reg2 = /^159\d{4,8}$/;
  8. var reg3 = /^0\d{10,11}$/;
  9. var my = false;
  10. if (reg0.test(s))my=true;
  11. if (reg1.test(s))my=true;
  12. if (reg2.test(s))my=true;
  13. if (reg3.test(s))my=true;
  14. if(s!="")
  15. {
  16. if (!my)
  17. {
  18. alert('請輸入正確的手機號碼');
  19. object.value="";
  20. object.focus();
  21. }
  22. }
  23. }
//校驗手機號碼:必須以數字開頭,除數字外,可含有“-”
             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();
                    }
                }
            }



5、郵編

Js代碼
  1. //校驗(國內)郵政編碼
  2. function isPostalCode(object)
  3. {
  4. var s =document.getElementById(object.id).value;
  5. var pattern =/^[0-9]{6}$/;
  6. if(s!="")
  7. {
  8. if(!pattern.exec(s))
  9. {
  10. alert('請輸入正確的郵政編碼');
  11. object.value="";
  12. object.focus();
  13. }
  14. }
  15. }
 //校驗(國內)郵政編碼
            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、日期

Js代碼
  1. //校驗日期
  2. function isdate(object)
  3. {
  4. var s =document.getElementById(object.id).value;
  5. 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])))))?$/;
  6. if(s!="")
  7. {
  8. if(!pattern.exec(s))
  9. {
  10. alert('請輸入正確的日期');
  11. object.value="";
  12. object.focus();
  13. }
  14. }
  15. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章