protected void Page_Load(object sender, EventArgs e)
{
    // 替三個 TextBox 加上 JavaScript 函數呼叫的功能
    TextBox1.Attributes["onBlur"] = "calc();";
    TextBox2.Attributes["onBlur"] = "calc();";
    TextBox3.Attributes["onBlur"] = "calc();";
}
----------------------------------------------------------------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>未命名頁面</title>
    <script type="text/javascript">        
        function calc() {
            var re = /^\d+$/;   // 驗證只能輸入數字的 Regular Expression
            intTotal = 0;
            intTextBox1 = 0;
            intTextBox2 = 0;
            intTextBox3 = 0;
            
            if (document.all.TextBox1.value != '')
            {
                obj = document.all.TextBox1;
               if (obj.value!='' && !re.test(obj.value))                
               {
                   document.all.Label1.innerText = '本欄位只能輸入數字';
                  document.all.TextBox1.select();
                  
                  // 若頁面中,有寫入資料庫功能的「確定」按鈕,可在這將其設為唯讀
                  // document.all.FormView1_btnInsertConfirm.disabled = true;
                  
                  return false;
               }
               else
               {
                   document.all.Label1.innerText = ''; // 若使用者改為只輸入數字,則清除 Label1 中的錯誤訊息
                   
                   // 若頁面中,有寫入資料庫功能的「確定」按鈕,可在這解除唯讀
                  // document.all.FormView1_btnInsertConfirm.disabled = false;
                   
                    intTextBox1 = eval(document.all.TextBox1.value); 
                }
            }
            else
            {
                document.all.Label1.innerText = '';     // 若使用者把 TextBox1 清空,則清除 Label1 中的錯誤訊息
            }
            
            if (document.all.TextBox2.value != '')
            {
                obj = document.all.TextBox2;
               if (obj.value!='' && !re.test(obj.value))                
               {
                   document.all.Label2.innerText = '本欄位只能輸入數字';
                  document.all.TextBox2.select();
                  
                  // 若頁面中,有寫入資料庫功能的「確定」按鈕,可在這將其設為唯讀
                  // document.all.FormView1_btnInsertConfirm.disabled = true;
                  
                  return false;
               }
               else
               {
                   document.all.Label2.innerText = ''; // 若使用者改為只輸入數字,則清除 Label2 中的錯誤訊息
                   
                   // 若頁面中,有寫入資料庫功能的「確定」按鈕,可在這解除唯讀
                  // document.all.FormView1_btnInsertConfirm.disabled = false;
                  
                    intTextBox2 = eval(document.all.TextBox2.value); 
                }
            }
            else
            {
                document.all.Label2.innerText = '';     // 若使用者把 TextBox2 清空,則清除 Label2 中的錯誤訊息
            }
            
            if (document.all.TextBox3.value != '')
            {
                obj = document.all.TextBox3;
               if (obj.value!='' && !re.test(obj.value))                
               {
                   document.all.Label3.innerText = '本欄位只能輸入數字';
                  document.all.TextBox3.select();
                  
                  // 若頁面中,有寫入資料庫功能的「確定」按鈕,可在這將其設為唯讀
                  // document.all.FormView1_btnInsertConfirm.disabled = true;
                  
                  return false;
               }
               else
               {
                   document.all.Label3.innerText = ''; // 若使用者改為只輸入數字,則清除 Label3 中的錯誤訊息
                   
                   // 若頁面中,有寫入資料庫功能的「確定」按鈕,可在這解除唯讀
                  // document.all.FormView1_btnInsertConfirm.disabled = false;
                  
                    intTextBox3 = eval(document.all.TextBox3.value); 
                }
            }
            else
            {
                document.all.Label3.innerText = '';     // 若使用者把 TextBox3 清空,則清除 Label3 中的錯誤訊息
            }
            
                
            intTotal = intTextBox1 + intTextBox2 + intTextBox3;     // 加總後的數字
            document.all.LabelTotal.innerText = intTotal;           // 顯示三個 TextBox 加總後的數字
        }    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Label ID="Label1" runat="server" ForeColor="Red" Font-Size="Small"></asp:Label><br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><asp:Label ID="Label2" runat="server" ForeColor="Red" Font-Size="Small"></asp:Label><br />
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><asp:Label ID="Label3" runat="server" ForeColor="Red" Font-Size="Small"></asp:Label><br />
        <br />
        三個 TextBox 的數字加總為:
        <asp:Label ID="LabelTotal" runat="server"></asp:Label><br />
    </div>
    </form>
</body>
</html>
 

解决方案 »

  1.   

    或者:  可以用Javascript对文本框进行检查,过滤掉非0-9的字符。
       <script language="javascript" event="onkeydown" for="document">
       if(event.srcElement.name=='TextBox1')
       {
       if(!KeyIsNumber(event.keyCode))
       {
       return false;//这句话最关键
       }
       }
      </script>
      <script language="javascript">
      function KeyIsNumber(KeyCode)
      {
       //如果输入的字符是在0-9之间,或者是backspace、DEL键
       if(((KeyCode>47)&&(KeyCode<58))||(KeyCode==8)||(KeyCode==46))
       {
       return true;
       }
       else
       {
       return false;
       }
      }
      </script>aspx">
      

  2.   

    用正则表达式啊
    using System.Text.RegularExpressions; 
    string valEx = @"^\d+$";
     
    if (!Regex.IsMatch(txtVal.Text.Trim(), valEx)) 

    txtVal.Focus(); 
    return ;