移動端監聽輸入手機號以及判斷手機號有效


項目案例需求如,輸入/綁定正確的手機號才能下載軟件,輸入手機號發送驗證碼的功能等;

如下代碼可以實現基本功能:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <style>
        .gray-btn{
            background: #ccc;
        }
        .blue-btn{
            background: #09f;
        }
    </style>
</head>
<body>
<input type="tel" name="mobile" id="getNum" value="" placeholder="填寫手機號碼" autocomplete='off'/>
<input type="button" name="button" class="gray-btn downloadBtn" value="去下載**APP" disabled/>
<script src="jquery-1.7.2.min.js"></script>
<script>
    //監聽輸入手機號
    $(document).on('input propertychange','#getNum',function (e) {
        if (e.type === "input" || e.orignalEvent.propertyName === "value") {
            if(this.value.length == 11){
                var myreg = /^1\d{10}$/;
                if(!myreg.test(this.value)){
                    common.tips({msg:'請輸入正確手機號'});
                    return;
                }
    
                $('.downloadBtn').removeClass('gray-btn').addClass('blue-btn');
                $('.downloadBtn').attr("disabled", false);
            }else{
                $('.downloadBtn').addClass('gray-btn').removeClass('blue-btn');
                $('.downloadBtn').attr("disabled", true);
            }
        }
    })
</script>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM