<input type='text' id="s1" onblur="check()"/><script>
function check()
{
var a=/^[1-9]\d{1,4}\.\d{1,4}$/g;
var b=document.getElementById("s1").value;
if(b.match(a)==null)
{
alert('error');
}
}
</script>

解决方案 »

  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>
    </head>
    <script language="javascript">
    function checkNum(num)
    {
    if(isNaN(num))
    {
    alert("not a num");
    return false;
    }
    var num1 = parseInt(num*10000) + "";
    var num2 = num*10000 + "";

    if(num1 != num2)
    {
    alert("error");
    }
    }</script>
    <body><input onblur="javascript:checkNum(this.value)" />
    </body>
    </html>
      

  2.   

    我不想提示error,我的控制只是让小数点后最多4位数字。
      

  3.   

    用正则replace
    var a=/\.\d{4,}/g;
    var b=表单的值 
    b.replace(a,"")
      

  4.   


    <SCRIPT>
    var num="qe";
    alert(num || 1);
    function getHtmlText(intFlag)
    {
    var marquee="<MARQUEE style=\"width:220px;font-size:12px;\" scrollAmount=1 bgcolor=#AAEEAA onmousemove=this.stop() onmouseout=this.start()>";
    marquee += "<A title=希望能在这里认识更多的朋友 href=\"http://hotesion.51.com/\" target=_blank>我的51博客</A> ";
    marquee += "<A title=这里的表演更精彩 href=\"http://houtingsong163.blog.163.com/\" target=_blank>我的网易博客</A> ";
    marquee += "<A title=这就是我的大学 href=\"http://www.99sushe.com/hotesion\" target=_blank>我的大学生生活</A> ";
    marquee += "<A title=这就是我和讯博客 href=\"http://hexun.com/monkeyhts\" target=_blank>我的和讯博客</A> ";
    marquee += "<A title=这就是我和讯交友 href=\"http://love.hexun.com/detail.aspx?id=LM7XKCrvDuY%3d\" target=_blank>我的和讯交友</A>";
    marquee += "</MARQUEE>"; var buttonHTML = "<div>\n";
    buttonHTML += "<INPUT name=\""+intFlag+"\" onclick=\"doRunHotesionCode(this.name)\" type=\"button\" value=\"运行代码\"/>\n";
    buttonHTML += "<INPUT name=\""+intFlag+"\" onclick=\"doCopyHotesionCode(this.name)\" type=\"button\" value=\"复制代码\"/>\n";
    buttonHTML += "<INPUT name=\""+intFlag+"\" onclick=\"doSelectHotesionCode(this.name)\" type=\"button\" value=\"全选代码\"/>\n";
    buttonHTML += "<INPUT name=\""+intFlag+"\" onclick=\"doSaveHotesionCode(this.name)\" type=\"button\" value=\"保存代码\"/>\n";
    buttonHTML += "<INPUT name=\""+intFlag+"\" onclick=\"doClearHotesionCode(this.name)\" type=\"button\" value=\"清除代码\"/>\n";
    buttonHTML += "<INPUT name=\""+intFlag+"\" onclick=\"addCollection()\" type=\"button\" value=\"加入收藏\"/>\n";
    buttonHTML += "<INPUT name=\""+intFlag+"\" onclick=\"setHomePage(this)\" type=\"button\" value=\"设为首页\"/>\n<br>";
    buttonHTML += "<INPUT name=\""+intFlag+"\" onclick=\"operURL('http://hotesion.51.com')\" type=\"button\" value=\"51空间\" title=\"到我的51空间,给我留言\"/>\n";
    buttonHTML += "<INPUT name=\""+intFlag+"\" onclick=\"operURL('http://houtingsong163.blog.163.com')\" type=\"button\" value=\"163空间\" title=\"到我的163空间,给我留言\"/>\n";
    buttonHTML += "<INPUT name=\""+intFlag+"\" onclick=\"operURL('http://www.99sushe.com/hotesion')\" type=\"button\" value=\"99宿舍空间\" title=\"到我的大学空间,给我留言\"/>\n";
    buttonHTML += "<INPUT name=\""+intFlag+"\" onclick=\"operURL('http://hexun.com/monkeyhts')\" type=\"button\" value=\"和讯网空间\" title=\"到我的和讯空间,给我留言\"/>\n";
    buttonHTML += "<INPUT name=\""+intFlag+"\" onclick=\"location.href='mailto:[email protected]'\" type=\"button\" value=\"发邮件给我\" title=\"您可以发邮件给我,邮件地址是: [email protected]\"/>\n";
    buttonHTML += marquee+"</div>";
    return buttonHTML;
    }function writeOperatorButton(buttonNameFlag)
    {
    document.write(getHtmlText(buttonNameFlag));
    }
    //通过标签的id获取页面文档的一个标签对象
    function getPageDocTagObjById(idValue) {
    if (document.getElementById){
    return document.getElementById(idValue);
    } else if (document.all) {
    return document.all[idValue];
    } else if (document.layers) {
    return document.layers[idValue];
    } else {
    return null;
    }
    }
    //运行代码的函数
    function doRunHotesionCode(buttonName){
      var code=getPageDocTagObjById("hotesionTextAreaCode_"+buttonName).value;//即要运行的代码。
      var newwin=window.open('','','');  //打开一个窗口并赋给变量newwin。
      newwin.opener = null // 防止代码对论谈页面修改
      newwin.document.write(code);  //向这个打开的窗口中写入代码code,这样就实现了运行代码功能。
      newwin.document.close();
    }//复制代码的函数
    function doCopyHotesionCode(buttonName){ 
    if (document.all){
     textRange = getPageDocTagObjById("hotesionTextAreaCode_"+buttonName).createTextRange(); 
     textRange.execCommand("Copy"); 
     alert("代码已经复制到剪切板");
    }else{
     alert("此功能只能在IE上有效nn请在文本域中用Ctrl+A选择再复制")
    }
    }
    //复制代码的函数
    function doSelectHotesionCode(buttonName){
    getPageDocTagObjById("hotesionTextAreaCode_"+buttonName).select(); 
    }
    //另存代码的函数
    function doSaveHotesionCode(buttonName){
    var winname = window.open('','','width=0,height=0,top=200,left=200px');
    document.open('text/html', 'replace');
    document.write(getPageDocTagObjById("hotesionTextAreaCode_"+buttonName).value);
    document.execCommand('saveas',false,'CSS动态滑动菜单.html');
    winname.close();
    }
    //另存代码的函数
    function doClearHotesionCode(buttonName){
    getPageDocTagObjById("hotesionTextAreaCode_"+buttonName).value="";
    }//加入收藏代码函数
    function addCollection()
    {
    window.external.AddFavorite(location.href,document.title);
    }
    //设置为主页代码函数
    function setHomePage(obj)
    {
    obj.style.behavior="url(#default#homepage)";
    obj.setHomePage(location.href);
    }
    //设置为主页代码函数
    function operURL(urlStr)
    {
    window.open(urlStr,"","");
    //window.location.target="_blank";
    //location.href=urlStr;
    }
    </SCRIPT>
    <!--=====================================================HTML start=========================================================-->
    <div>
    看看下面这个代码吧<BR>
    <TEXTAREA id="hotesionTextAreaCode_10" flag="html_code" rows="15" cols="95">
    <div id="inputNumbers">
        <input type='text' value="12345.3456"/><span></span><br>
        <input type='text' value="02345.3456"/><span></span><br>
        <input type='text' value="12345.345"/><span></span><br>
        <input type='text' value="12345."/><span></span><br>
        <input type='text' value="123.3456"/><span></span><br>
        <input type='text' value="02345."/><span></span><br>
        <input type='text' value="12045.0456"/><span></span><br>
        <input type='text' value="12045.3450"/><span></span><br>
    </div>
    <div onclick="checkInputValue()" style="float:left;width:70px;cursor: hand; border: solid 2px red;">检查输入</div>
    <script>
    function checkInputValue()
    {
        var inputDiv = document.getElementById("inputNumbers");
        var inputArr = inputDiv.getElementsByTagName("input");
        var spanArr = inputDiv.getElementsByTagName("span");    var reg=/^[1-9]\d{1,4}\.\d{1,4}$/g;
        var len = inputArr.length;
        for(var i = 0; i < len; i++)
            spanArr[i].innerHTML = "<font color='red'>输入" + (reg.test(inputArr[i].value) ? "正确" : "有误") + "</font>";
    setTimeout("hiddenSpanText()",5000);
    }
    function hiddenSpanText()
    {
        var inputDiv = document.getElementById("inputNumbers");
        var spanArr = inputDiv.getElementsByTagName("span");
        var len = spanArr.length;
    for(var i = 0; i < len; i++)
            spanArr[i].innerHTML = "";
    }
    </script>
    </TEXTAREA>
    </div>
    <!--=====================================================HTML end=========================================================-->
    <script>
    //设置TEXTAREA的样式
    function setTextAreaStyle(textAreaObj)
    {
    textAreaObj.style.borderTop="#0099cc 1px solid";
    textAreaObj.style.borderBottom="#0099cc 1px solid";
    textAreaObj.style.borderLeft="#0099cc 1px solid";
    textAreaObj.style.borderRight="#0099cc 1px solid";
    textAreaObj.style.paddingTop="5px";
    textAreaObj.style.paddingBottom="5px";
    textAreaObj.style.paddingLeft="5px";
    textAreaObj.style.paddingRight="5px";
    textAreaObj.style.fontSize="12px";
    textAreaObj.style.margin="5px";
    textAreaObj.style.color="#000000";
    textAreaObj.style.lineHeight="18px";
    textAreaObj.style.fontFamily="'Courier New', 'Courier', 'mono'";
    textAreaObj.style.backgroundColor="#ddedfb";
    }var htmlTextAreaArr = document.getElementsByTagName("textarea");
    var textAreaCount = 0;
    for(i=0;i<htmlTextAreaArr.length;i++)
    {
    var textAreaTemp = htmlTextAreaArr[i];
    if(textAreaTemp.flag=="html_code")
    {
    setTextAreaStyle(htmlTextAreaArr[i]);
    textAreaTemp.insertAdjacentHTML("afterEnd",getHtmlText(textAreaCount));
    textAreaCount++;
    }}
    </script>
      

  5.   

    看看下面这个代码吧,直接复制运行<div id="inputNumbers">
        <input type='text' value="12345.3456"/><span></span><br>
        <input type='text' value="02345.3456"/><span></span><br>
        <input type='text' value="12345.345"/><span></span><br>
        <input type='text' value="12345."/><span></span><br>
        <input type='text' value="123.3456"/><span></span><br>
        <input type='text' value="02345."/><span></span><br>
        <input type='text' value="12045.0456"/><span></span><br>
        <input type='text' value="12045.3450"/><span></span><br>
    </div>
    <div onclick="checkInputValue()" style="float:left;width:70px;cursor: hand; border: solid 2px red;">检查输入</div>
    <script>
    function checkInputValue()
    {
        var inputDiv = document.getElementById("inputNumbers");
        var inputArr = inputDiv.getElementsByTagName("input");
        var spanArr = inputDiv.getElementsByTagName("span");    var reg=/^[1-9]\d{1,4}\.\d{1,4}$/g;
        var len = inputArr.length;
        for(var i = 0; i < len; i++)
            spanArr[i].innerHTML = "<font color='red'>输入" + (reg.test(inputArr[i].value) ? "正确" : "有误") + "</font>";
    setTimeout("hiddenSpanText()",5000);
    }
    function hiddenSpanText()
    {
        var inputDiv = document.getElementById("inputNumbers");
        var spanArr = inputDiv.getElementsByTagName("span");
        var len = spanArr.length;
    for(var i = 0; i < len; i++)
            spanArr[i].innerHTML = "";
    }
    </script>