我要用JavaScript实现文本框只能输入数字 和 :(冒号)最好有详细的代码,谢谢!

解决方案 »

  1.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <body>
    <input onkeyup='checkme(this)' type="text" />
    <script>

    function checkme(el){
    el.value=el.value.replace(/[^0-9:]/g,'');
    }
    </script>
    </body>
    </html>
      

  2.   

    <html><head><title></title>
    <script type="text/javascript">
    function show(obj)
    {
        var reg=/^[\d|:]*$/g
        var patt1 = new RegExp(reg);    alert(patt1.test(obj));
    }
    </script>
    </head><body>
    <input type="text" onblur="show(this.value)">
    </body></html>
      

  3.   

    完整代码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head>
    <body>
    <form action="http://www.baidu.com" id="myForm">
    输入:<input type="text" id="txtName" /><br>
    <input type="button" id="sub" value="提交" onclick="sb()" />
    </form>
    </body>
    </html>
    <script language="javascript">
    function sb()
    {
    var pattern=/^(\d|:)+$/;
    var name=document.getElementById('txtName').value;
    if(!pattern.test(name)){
    alert("输入不合法");
    document.getElementById('txtName').focus();
    document.getElementById('txtName').select();
    }else 
    {
    document.getElementById('myForm').submit();
    }
    }
    </script>