正则匹配一个字符串为手机号码或者空怎么写,  主要是 或者为空 这部分不会写

解决方案 »

  1.   

    <head>
        <title>Test</title>
    </head>
    <body>
    <input type="text" id="tel"/>
    <input type="button" value="button" id="btn" />
    <script type="text/javascript" >
    var inputbox = document.getElementById('tel')
    document.getElementById('btn').onclick = function(){
    inputbox.value = check(inputbox.value);
    }
    function check(str){
    var reg = /^1(3|5|8)\d{9}$/
    if(reg.test(str)){
    alert('It\'s right!');
    return str;
    }else{
    alert('It\'s wrong!');
    return '';
    }
    }
    </script>
    </body>
    </html>
      

  2.   

     在楼上的基础上加了个是否为空的判断
    <body>
        <input type="text" id="tel"/>
        <input type="button" value="button" id="btn" />
        <script type="text/javascript" >
            var inputbox = document.getElementById('tel')
            document.getElementById('btn').onclick = function(){
                inputbox.value = check(inputbox.value);
            }
            function check(str){
                var reg = /^1(3|5|8)\d{9}$/
                if(reg.test(str)){
                    alert('手机号');
                    return str;
                }else if(str==""){
    alert("为空");
    return "";
    }else{
                    alert('It\'s wrong!');
                    return '';
                }
            }
        </script>
    </body> 
      

  3.   


     在楼上的基础上加了个是否为空的判断
    <body>
        <input type="text" id="tel"/>
        <input type="button" value="button" id="btn" />
        <script type="text/javascript" >
            var inputbox = document.getElementById('tel')
            document.getElementById('btn').onclick = function(){
                inputbox.value = check(inputbox.value);
            }
            function check(str){
                var reg = /^1(3|5|8)\d{9}$/
                if(reg.test(str)){
                    alert('手机号');
                    return str;
                }else if(str==""){
    alert("为空");
    return "";
    }else{
                    alert('It\'s wrong!');
                    return '';
                }
            }
        </script>
    </body> 
      

  4.   


    var str1 = "13454567867";
    var str2 = "";
    alert(/^(1(3|5|8)\d{9})?$/.test(str1)); //true
    alert(/^(1(3|5|8)\d{9})?$/.test(str2)); //true这样呢?
      

  5.   

    蛋得,我还以为你是要判断一个字符串为手机号,不符合要求就清空呢。。<head>
        <title>Test</title>
    </head>
    <body>
        <input type="text" id="tel"/>
        <input type="button" value="button" id="btn" />
        <script type="text/javascript" >
            var inputbox = document.getElementById('tel')
            document.getElementById('btn').onclick = function(){
                inputbox.value = check(inputbox.value);
            }
            function check(str){
                var reg = /^(1(3|5|8)\d{9})?$/
                if(reg.test(str)){
                    alert('为手机号或为空');
                }else{
                    alert('其它');
                }
            }
        </script>
    </body>
    </html>
      

  6.   

    1./^(?:1(?:3|5|8)\d{9})?$/
    2./^(1(3|5|8)\d{9})?$/
    要么匹配,要么什么都不匹配(那就是空)
    匹配 pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用 "或" 字符 (|) 来组合一个模式的各个部分是很有用。例如, 'industr(?:y|ies) 就是一个比 'industry|industries' 更简略的表达式。那么(?:13\d|15\d)也就是要在这里面选择匹配13+0~9的数字一个或15+0~9的数字
    study