通用的表單驗證,郵箱身份證號和電話

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/c782699991/article/details/78717147


<HTML>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <BODY>
        <form action="http://www.baidu.com" onsubmit="return toVaild()">
            <input type="text" id="ff">

<input type="text" name="docNo" id="docNo"  onblur="isIdCardNo();" />

<input type="text" name="birthDate" id="birthDate"  readonly="readonly" />

<input type="text" name="phoneNo" id="phoneNo"  onblur="checkMobile();" />

<input type="text" name="email" id="email"  onblur="checkEmail()" />

            <input type="submit" id="submit" value ="提交"/>
        </form>
    </BODY>
        <script language="javascript">
           //增加身份證驗證
    function isIdCardNo() {
    var num = $("#docNo").val();
        var factorArr = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
        var parityBit = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");
        var varArray = new Array();
        var intValue;
        var lngProduct = 0;
        var intCheckDigit;
        var intStrLen = num.length;
        var idNumber = num;
        // initialize
        if(intStrLen == 0){
        return true;
        }
        if ((intStrLen != 15) && (intStrLen != 18)) {
        $("#docNo").tips({
side:3,
           msg:'請輸入正確的身份證號',
           bg:'#AE81FF',
           time:2
       });
$("#docNo").focus();
        return false;
        }
        
        //通過身份證號獲得出生年月
        if($.trim($("#docNo").val()) != "") {
            var date = "";
             if($("#docNo").val().length == 15) {
                date =  '19'+$("#docNo").val().substr(6,2)+'-'+$("#docNo").val().substr(8,2)+'-'+$("#docNo").val().substr(10,2);
             } else if ($("#docNo").val().length == 18) {
                 date =  $("#docNo").val().substr(6,4)+'-'+$("#docNo").val().substr(10,2)+'-'+$("#docNo").val().substr(12,2);
            }
            $("#birthDate").val(date);
            
            var now_time = new Date();
    var birthday = new Date(date);
    var newdate = now_time.getTime() - birthday.getTime();
    var age = Math.ceil(newdate/1000/60/60/24/365);
    if(age<18 ||age>65){
    $("#docNo").tips({
    side:3,
               msg:'請重新輸入身份證號,年齡不能小於18或大於65',
               bg:'#AE81FF',
               time:2
           });
    $("#docNo").focus();
    return false;
    }            
         }    
        return true;
    }
   



//驗證輸入電話
function checkMobile(str){

var PHONE = $.trim($("#phoneNo").val());
var checkPhone=true;
var re= /(^1[3|5|8][0-9]{9}$)/;
if(PHONE == ""){
        return true;
        }
        if(!re.test(PHONE)){  
        $("#phoneNo").tips({
side : 1,
msg : "請輸入正確的手機號",
bg : '#FF5080',
time : 2
});
$("#phoneNo").focus();
return false;
        }
       
        return checkMobile;
        }  
//驗證輸入郵箱是否正確
function checkEmail(){ 
var str = $("#email").val();
        var re=/\w+[@]{1}\w+[.]\w+/;
        if(str == ""){
        return true;
        }
        if(!re.test(str)){  
        $("#email").tips({
    side:3,
               msg:'請輸入正確的郵箱',
               bg:'#AE81FF',
               time:2
           });
    $("#email").focus();
    return false;
        }
        return true;
    } 
    </script>
</HTML>

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