input 手机号

关于input手机号的验证

一、手机号的判断方法:

function isPhoneTel(n){
    var reg = /^1[3|4|5|8]\d{9}$/;
    if(!!(reg.test(n))){
        return true;
    }
    else{
        return false;
    }
}

 

二、input绑定事件

phone: function(){
    var _this = this;

    // 只可以输入数字、删除等键
    _this.kphone.onkeydown = function(e){
        e = e || window.event;
        var code = e.keyCode;  
        if(!((code >= 48 && code <= 57) || (code >= 96 && code <= 105) || code === 8 || code === 13 || code===46 || code === 37 || code === 39)){
            return false;
        }
    }

    // 当是11位手机号时触发回调函数
    _this.kphone.onkeyup = function(e){ 
        e = e || window.event;
    } 

    // 当失去焦点时判断是不是正确手机号
    _this.kphone.onblur = function(){if(_this.isPhoneTel(_this.kphone.value)){
            _this.ktip.innerHTML = "您的手机号:"+_this.kphone.value;
        } else{
            _this.ktip.innerHTML = "请输入正确的手机号码!";
        }
    }
}

 

例子:

 
posted @ 2013-09-01 22:10  前端咖  阅读(5191)  评论(0编辑  收藏  举报