前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[基础]电话/手机常见验证的Javascript示例

[基础]电话/手机常见验证的Javascript示例

作者头像
菩提树下的杨过
发布2018-01-22 17:07:27
8300
发布2018-01-22 17:07:27
举报
代码语言:javascript
复制
电话号码:<input type="text" id="TelPrefix" size="4" MaxLength="4"> - <input type="text" id="TelNumber" style="width:88px"> <button id="btnTestTel" onClick="return TestTel()">测试电话号码</button><br/>

手机号码:<input type="text" id="Mobile"> <button id="btnTestMobile" onClick="return TestMobile()">测试手机号码</button><br/><br/>

建议手机号码与电话号码至少请输入一项<br/><br/>

<button id="btnTest" onClick="return Test()">测试手机号码</button>



<script type="text/javascript">

    String.prototype.Trim = function() {  

        var m = this.match(/^\s*(\S+(\s+\S+)*)\s*$/);  

        return (m == null) ? "" : m[1];  

    }


    String.prototype.isMobile = function() {  

        return (/^(?:13\d|15[89])-?\d{5}(\d{3}|\*{3})$/.test(this.Trim()));  

    } 

    String.prototype.isTel = function()

    {   

        return (/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(this.Trim()));

    }



    function TestMobile()

    {

        var oMobile = document.getElementById("Mobile");

        if (!oMobile.value.isMobile())

        {

            alert("请输入正确的手机号码");

            oMobile.focus();

            oMobile.style.backgroundColor="#ff9";

            return false;

        }

        else

        {

            alert(oMobile.value);

        }

        return true;

    }





    function TestMobileEx()

    {

        var oMobile = document.getElementById("Mobile");

        if (!oMobile.value.isMobile())

        {            

            return false;

        }        

        return true;

    }



    function TestTel()

    {

        var oTelPrefix = document.getElementById("TelPrefix");

        var oTelNumber = document.getElementById("TelNumber");

        var sTel = oTelPrefix.value + "-" + oTelNumber.value;

        

        if (!sTel.isTel())

        {

            alert("请输入正确的电话号码");

            oTelPrefix.focus();

            oTelPrefix.style.backgroundColor="#ff9";

            oTelNumber.style.backgroundColor="#ff9";

            return false;

        }

        else

        {

            alert(sTel);

        }



        return true;

    }



    function TestTelEx()

    {

        var oTelPrefix = document.getElementById("TelPrefix");

        var oTelNumber = document.getElementById("TelNumber");

        var sTel = oTelPrefix.value + "-" + oTelNumber.value;

        

        if (!sTel.isTel())

        {            

            return false;

        }

        return true;

    }





    function Test()

    {

        if (TestTelEx()||TestMobileEx())

        {

            alert("验证通过");

        }

        else

        {

            alert("手机,电话至少输入一项");

            document.getElementById("TelPrefix").focus();

            document.getElementById("TelPrefix").style.backgroundColor="#ff9";

            return false;

        }

        return true;

    }

</script>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2008-01-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档