检查一段字符串是否全由数字组成<script language="Javascript"><!--
function checkNum(str){return str.match(/\D/)==null}
alert(checkNum("1232142141"))
alert(checkNum("123214214a1"))
// --></script>

解决方案 »

  1.   

    <html>
    <head>
    <script language=javascript>
    function check(){
    textvalue=form1.txtTest.value;
    for (var i=0;i<textvalue.length;i++){
        if ("0123456789".indexOf(textvalue.substring(i,i+1))==-1){
        alert("请全部输入数字!");
        document.form1.txtTest.value='';
        document.form1.txtTest.focus();
        break;
        }
        else{
        continue;
        }
    }
        if (textvalue.charAt(0)=="0"){
        alert ("第一个数字不可以为0;")
        document.form1.txtTest.value='';
        document.form1.txtTest.focus();
        }}
    </script>
    </head>
    <body>
    <form name="form1" action="">
    <input type=text size=8  name="txtTest">
    <input type="button" value="按钮" name="B1" onclick="check()">
    </form >
    </body>
    </html>