在GridView 有个TextBox列 何用js 判断输入是否是数字 并且精确到2位小数,如果不对TextBox 变为红色并有提示框<asp:GridView ID="GVRoomList" runat="server" AllowSorting="True" PageSize="100" CellPadding="4" ForeColor="Black" GridLines="None" Width="100%" AutoGenerateColumns="False">
<Columns>
<asp:templatefield headertext="金额">
<itemtemplate>
<asp:TextBox ID="txtPRICE" runat="server" Width="60" BorderStyle="Groove"></asp:TextBox>
</itemtemplate>
<ItemStyle Width="60px" />
<HeaderStyle Font-Size="Small" ForeColor="Black" />
</asp:templatefield>
</Columns>
</asp:GridView>我不知道如何去实现,请教下各位高手 非常感谢!

解决方案 »

  1.   

    var textbox = document.getElementById("txtPRICE")
    if(textbox.value为数字)
    else
    {textbox.style.backgroudcolor='red';}
      

  2.   

    写一个JS函数.
    <script>
      function JS()
      {
         //根据需求进行写...
      }
    </script>然后在文本onblur="JS()"
      

  3.   

    <asp:templatefield headertext="金额"> 
    <itemtemplate> 
    <asp:TextBox ID="txtPRICE" runat="server" Width="60" BorderStyle="Groove" onblue="JS"> </asp:TextBox> 
    </itemtemplate> 
    然后
    <script type="text/javascript"> 
      function JS() 
      { 
        //根据需求进行写... 
      } 
    </script> 
      

  4.   

    在TextBox中添加onchange="textCheck(this)" function textCheck(text1)
     {    
        var text1;   
        var sTest=/\d+.\d{2}$/;
        if(sTest.test(text1.value)==false)
        {
            alert("Format error!")
        }
     }
      

  5.   

    var textbox = document.getElementById("txtPRICE") 
    if(textbox.value为数字) 
    else 
    {textbox.style.backgroudcolor='red';}
    这样不行的,我那是个GridView 列 有多个textbox ID是不同的 而且是<asp:TextBox>控件  这样写document.getElementById("txtPRICE") 报缺少对象错误
     
      

  6.   

    在TextBox中添加onchange="textCheck(this)" var textbox = document.getElementById("txtPRICE") function textCheck(text1) 
    {    
        var text1;  
        var sTest=/\d+.\d{2}$/; 
        if(sTest.test(text1.value)==false) 
        { 
            textbox.style.backgroudcolor='red';
        }

      

  7.   

    function IsDecimal(obj)
        {
            if(obj.value=="")return;
            var str=/^(\d+\d$)|(^[0-9]+d*\.\d{2}$)/;
            if(!str.test(obj.value))
            {
                window.alert("输入数字不合法,正确的格式为100或100.12");
                obj.style.cssText="background-clor:red";
                obj.select();
            }
            else
            {
                obj.style.cssText="";
            }
        } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Separator)
            {
                TextBox txtPrice = e.Row.FindControl("txtPRICE") as TextBox;
                if (txtPrice != null)
                {
                    txtPrice.Attributes.Add("onblur", "IsDecimal(this)");
                }
            
            }
        }
      

  8.   

    document.getElementById("<%=textboxID.ClientID%>") TextBox的客户端ID生成页面 查看一下原文件就知道了可能会少了this.....