jquery验证手机号码和固定电话号码

<pre name="code" class="javascript"> //验证手机号码或者电话号码  
    function checkContactNumber() {  
        $("#error").css("display", "none");  
        var mobile = $.trim($("#ContactNumber").val());  
        var isMobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(14[0-9]{1}))+\d{8})$/;  
        var isPhone = /^(?:(?:0\d{2,3})-)?(?:\d{7,8})(-(?:\d{3,}))?$/;;  
        var error = "<label id=\"error\" class=\"validate_input_error\">请正确填写电话号码,例如:13511111111或010-11111111</label>";  
        //如果为1开头则验证手机号码  
        if (mobile.substring(0, 1) == 1) {  
            if (!isMobile.exec(mobile) && mobile.length != 11) {  
                $("#ContactNumber").after(error);  
                $("#ContactNumber").focus();  
                return false;  
            }  
        }  
        //如果为0开头则验证固定电话号码  
        else if (mobile.substring(0, 1) == 0) {  
            if (!isPhone.test(mobile)) {  
                $("#ContactNumber").after(error);  
                $("#ContactNumber").focus();  
                return false;  
            }  
        }  
        //否则全部不通过  
        else {  
            $("#ContactNumber").after(error);  
            $("#ContactNumber").focus();  
            return false;  
        }  
        return true;  
    }  

 

posted @ 2016-08-16 19:13  午时的海  阅读(926)  评论(0编辑  收藏  举报