rt

解决方案 »

  1.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If Not IsNumeric(Chr(KeyAscii)) Then
            KeyAscii = 0
        End If
    End Sub
      

  2.   

    Private Sub Text1_KeyPress(KeyAscii As Integer) 
        select case keyascii
            case 48 to 57 '数字
             case 8 '退格键
             case else
                keyascii=0
        end select
    End Sub
      

  3.   

    请参考如下代码,TEXTBOX只能输入数字。Private Sub Text1_KeyPress(KeyAscii As Integer)
     If KeyAscii <> 8 And KeyAscii <> 127 Then
            If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
      End If
    End Sub
      

  4.   

    '限制只输入数字的话还要考虑输入负号与输入小数点时的情况,我一般是输入结束后判断是否为数据格式:
    private sub text1_lostfocus()
        if trim(text1.text)<>"" then
            if not isnumeric(text1.text) then
                msgbox "请输入数值型数据!",48,"提示"
                text1.text=""
                text1.setfocus
            end if
        end if
    end sub
      

  5.   

    俺也来凑凑热闹,可输入负号、点号的版本
    Private Sub Text1_KeyPress(KeyAscii As Integer)
       Dim i As Integer
       Dim j As Integer
       If KeyAscii > 57 Or KeyAscii < 48 And KeyAscii <> 46 And KeyAscii <> 8 And KeyAscii <> 13 And KeyAscii <> 45 Then
          Text1.Locked = True
       ElseIf KeyAscii = 46 Then
          i = InStr(Text1.Text, ".")
          j = Len(Trim(Text1.Text))
          If j = 0 Then
             Text1.Locked = True
          ElseIf i = 0 Then
             Text1.Locked = False
          Else
             Text1.Locked = True
          End If
       ElseIf KeyAscii = 45 Then
          j = Len(Trim(Text1.Text))
          If j = 0 Then
             Text1.Locked = False
          Else
             Text1.Locked = True
          End If
       Else
          Text1.Locked = False
       End If
    End Sub
      

  6.   

        Dim lOldStytle As Long
        Dim ret As Long
        
        Const GWL_STYLE = (-16)
        Const ES_NUMBER = &H2000
        lOldStytle = GetWindowLong(Text1.hWnd, GWL_STYLE)
        lOldStytle = lOldStytle Or ES_NUMBER
        ret = SetWindowLong(Text1.hWnd, GWL_STYLE, lOldStytle)
      

  7.   


    正解,赞一个。我补一个完整版本吧:Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Const GWL_STYLE As Long = -16
    Private Const ES_NUMBER As Long = &H2000&Private Sub Form_Load()
        Dim lOldStytle As Long
        Dim ret As Long    lOldStytle = GetWindowLong(Text1.hwnd, GWL_STYLE)
        lOldStytle = lOldStytle Or ES_NUMBER
        ret = SetWindowLong(Text1.hwnd, GWL_STYLE, lOldStytle)
    End Sub
      

  8.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        
        If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> Asc(".") And KeyAscii <> 8 Then
            KeyAscii = 0
        End If
        
    End Sub
      

  9.   


    补充一点,不能输入两个小数点
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        
        If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> Asc(".") And KeyAscii <> 8 Then
            KeyAscii = 0
        End If
        If KeyAscii = Asc(".") And InStr(Text1.Text, ".") > 0 and  leThen
            KeyAscii = 0
        End If
        
    End Sub
      

  10.   


    '我的想法是,在改变内容的时间进行设置.
    '如果改变后还是数值型,就保存起来.
    '如果改变后不是数值型,就让他改为前一次的保存结果.Option ExplicitPrivate OldText As DoublePrivate Sub Text1_Change()
      If IsNumeric(Text1.Text) Then
         OldText = Text1.Text
         Text1.SelStart = Len(Text1.Text)
      Else
         Text1.Text = OldText
         Text1.SelStart = Len(Text1.Text)
      End If
    End Sub
      

  11.   

    利用正则表达式限制网页表单里的文本框输入内容:用正则表达式限制只能输入中文:onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"用正则表达式限制只能输入全角字符: onkeyup="value=value.replace(/[^\uFF00-\uFFFF] /g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"用正则表达式限制只能输入数字和英文:onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
      

  12.   

    Private Sub Text_GaoShu_KeyPress(KeyAscii As Integer)
        If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
            KeyAscii = 0
        End If
    End Sub
    这样也行
      

  13.   

    <asp:textbox class="Text"
    onkeypress="if (event.keyCode < 48 || event.keyCode >57) event.returnValue = false;"
    id="txtY_Revenue" style="TEXT-ALIGN: right" runat="server" Width="90%" MaxLength="12">
    </asp:textbox>
    说明: 此方法控制TextBox只收数字:0~9 , 也自可以定义其它可输入字符,如改成: 65~123,只允许输入: a~z和A~Z 等.
      

  14.   

    IsNumeric(Text1.Text)  检查输入的是否为数字,如果是数字返回TRUE
      

  15.   

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
            { 
                e.Handled = !(char.IsDigit(e.KeyChar) ¦ ¦(e.KeyChar == 8) ¦ ¦(e.KeyChar == 45) ¦ ¦(e.KeyChar ==46));  
            }上面是我在另外一个帖子的回复
    http://topic.csdn.net/u/20080805/14/6583cd12-8e2a-4a09-a1f5-acc2236d5345.html
      

  16.   

    只有VB的啊,有没有JAVA,或是.NET的解决方法啊 ?
      

  17.   

    原来是vb的,看错了,还是希望对你有帮助
    我19楼的回复是.NET的解决方案
      

  18.   


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">  
      
    <html>  
      
    <head>  
      
        <title></title>  
      
      
      
        <script type="text/javascript">  
      
        //正整数的验证   
      
    function valNum(ev)   
      
    {   
      
       if(window.event.shiftKey)   
      
            {   
      
               ev.returnValue = "";   
      
            }   
      
            else   
      
            {   
      
        var e = ev.keyCode;   
      
        //允许的有大、小键盘的数字,左右键,backspace, delete, Control + C, Control + V   
      
        if(e != 48 && e != 49 && e != 50 && e != 51 && e != 52 && e != 53 && e != 54 && e != 55 && e != 56 && e != 57 && e != 96 && e != 97 && e != 98 && e != 99 && e != 100 && e != 101 && e != 102 && e != 103 && e != 104 && e != 105 && e != 37 && e != 39 && e != 13 && e != 8 && e != 46 && e != 9)   
      
        {   
      
            if(ev.ctrlKey == false)   
      
            {   
      
                //不允许的就清空!   
      
                ev.returnValue = "";   
      
            }   
      
            else   
      
            {   
      
                //验证剪贴板里的内容是否为数字!   
      
                valClip(ev);   
      
            }   
      
        }   
      
        }   
      
    }   
      
    //验证剪贴板里的内容是否为数字!   
      
    function valClip(ev)   
      
    {   
      
        //查看剪贴板的内容!     
      
        try   
      
        {   
      
            var content = clipboardData.getData("Text");   
      
            if(content != null)   
      
            {   
      
                var test = parseInt(content);   
      
                var str = "" + test;   
      
                if(isNaN(test) == true)   
      
                {   
      
                    //如果不是数字将内容清空!   
      
                    clipboardData.setData("Text","");   
      
                }   
      
                else   
      
                {   
      
                    if(str != content)   
      
                        clipboardData.setData("Text", str);   
      
                }   
      
            }   
      
        }   
      
        catch(e)   
      
        {   
      
            //清空出现错误的提示!   
      
            alert("粘贴出现错误!");   
      
        }   
      
          
      
    }   
      
    //验证浮点数   
      
    function valFloat(ev,thisValue)   
      
    {   
      
         
      
    if(window.event.shiftKey==false)   
      
    {   
      
        if(thisValue!="")   
      
        {   
      
      
      
            var e = ev.keyCode;   
      
            var re=/\./g;   
      
            var arr;   
      
            if((arr = re.exec(thisValue)) == null)   
      
            {   
      
                    //允许的有大、小键盘的数字,左右键,backspace, delete, Control + C, Control + V,小数点   
      
                    if(e!=190 && e!=110&& e != 48 && e != 49 && e != 50 && e != 51 && e != 52 && e != 53 && e != 54 && e != 55 && e != 56 && e != 57 && e != 96 && e != 97 && e != 98 && e != 99 && e != 100 && e != 101 && e != 102 && e != 103 && e != 104 && e != 105 && e != 37 && e != 39 && e != 13 && e != 8 && e != 46 && e != 9)   
      
                    {   
      
                        if(ev.ctrlKey == false)   
      
                        {   
      
                            //不允许的就清空!   
      
                            ev.returnValue = "";   
      
                        }   
      
                        else   
      
                        {   
      
                            //验证剪贴板里的内容是否为浮点数!   
      
                            valFloatClip(ev);   
      
                         }   
      
                    }   
      
             }   
      
             else   
      
             {   
      
                        
      
                     //允许的有大、小键盘的数字,左右键,backspace, delete, Control + C, Control + V,小数点   
      
                    if(  e != 48 && e != 49 && e != 50 && e != 51 && e != 52 && e != 53 && e != 54 && e != 55 && e != 56 && e != 57 && e != 96 && e != 97 && e != 98 && e != 99 && e != 100 && e != 101 && e != 102 && e != 103 && e != 104 && e != 105 && e != 37 && e != 39 && e != 13 && e != 8 && e != 46 && e != 9)   
      
                    {   
      
                        if(ev.ctrlKey == false)   
      
                        {   
      
                            //不允许的就清空!   
      
                            ev.returnValue = "";   
      
                        }   
      
                        else   
      
                        {   
      
                            //验证剪贴板里的内容是否为浮点数!   
      
                            valFloatClip(ev);   
      
                         }   
      
                    }   
      
                   
      
              }   
      
      }   
      
      else   
      
      {   
      
             valNum(ev);   
      
      }   
      
     }   
      
     else   
      
     {   
      
        ev.returnValue = "";   
      
     }   
      
    }   
      
      
      
    //验证剪贴板里的内容是否为浮点数字!   
      
    function valFloatClip(ev)   
      
    {   
      
        //查看剪贴板的内容!     
      
        try   
      
        {   
      
            //如果不是数字将内容清空!   
      
            var content = clipboardData.getData("Text");   
      
            if(content != null)   
      
            {   
      
                
      
                var test = parseFloat(content);   
      
                var str = "" + test;   
      
                if(isNaN(test) == true)   
      
                {   
      
                    //如果不是数字将内容清空!   
      
                    clipboardData.setData("Text","");   
      
                }   
      
                else   
      
                {   
      
                    if(str != content)   
      
                        clipboardData.setData("Text", str);   
      
                }   
      
            }   
      
        }   
      
        catch(e)   
      
        {   
      
            //清空出现错误的提示!   
      
            alert("粘贴出现错误!");   
      
        }   
      
          
      
    }   
      
    //正则float   
      
    function validfloat(thisValue)   
      
    {   
      
        var rgExp=/((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))/;   
      
        var str=thisValue;   
      
       if(rgExp.exec(str))   
      
       {   
      
           return  true;   
      
       }   
      
       else   
      
       {   
      
           return false;   
      
       }   
      
    }   
      
        </script>  
      
      
      
    </head>  
      
    <body>  
      
        <form runat="server">  
      
            正整数<input id="Text1" type="text" onkeydown=" valNum(event);" style="ime-mode: Disabled;" />  
      
            浮点数<input id="Text2" type="text" onkeydown=" valFloat(event,this.value);" style="ime-mode: Disabled;" />  
      
        </form>  
      
    </body>  
      
    </html> 
      

  19.   

    最简单的就是
    onkeydown
    onkeypress
    这二个事件中判断
    如何不符合就不要嘛...
      

  20.   

    function isMath(math) {
    if (math.search(......) != -1)
    return true;
    else
    return false;
    }
      

  21.   

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                e.Handled = !(char.IsDigit(e.KeyChar) || (e.KeyChar == 8) );  
            }
      

  22.   

        If (KeyAscii < 48 Or KeyAscii > 57) Then
            KeyAscii = 0
            End If
        End I
      

  23.   


    private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
            { 
                e.Handled = !(char.IsDigit(e.KeyChar) ¦ ¦ (e.KeyChar == 8) );  
            } 
      

  24.   

    用正则表达式限制只能输入中文:onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))" 用正则表达式限制只能输入全角字符: onkeyup="value=value.replace(/[^\uFF00-\uFFFF] /g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))" 用正则表达式限制只能输入数字:onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" 用正则表达式限制只能输入数字和英文:onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" 
      

  25.   

    javascript 里面有个 isNaN 函数,不是可以直接判断吗,干吗还要搞这么复杂啊
      

  26.   

    Private Sub Text1_Change()
    If Len(Text1.Text) = 0 Then Exit Sub
    If Not IsNumeric(Text1.Text) Then
        Text1.Text = Mid(Text1.Text, 1, Len(Text1.Text) - 1)
        Text1.SelStart = Len(Text1.Text)
    End If
    End Sub
      

  27.   

    这种贴子也能上推荐啊,不知道版主怎么想的。上一段8年前的代码出来 Function ValiText(KeyIn As Integer, ValidateString As String, Editable As Boolean) As Integer
        '控制输入
        Dim ValidateList As String
        Dim KeyOut As Integer
        If Editable = True Then
             ValidateList = UCase(ValidateString) & Chr(8)
        Else
             ValidateList = UCase(ValidateString)
        End If
        If InStr(1, ValidateList, UCase(Chr(KeyIn)), 1) > 0 Then
            KeyOut = KeyIn
        Else
            KeyOut = 0
            Beep
        End If
        ValiText = KeyOut
    End Function
      

  28.   

    用Ajax多好啊,FilteredTextBoxExtender控件,设置为只能输入数字。
      

  29.   

    谢谢大家啦~~
    不好意思 分数不够分的了 只好给先答的而且思路与前面不同的了 
    再次谢谢每一位回帖的YDJM~~
      

  30.   

    在网页里让文本框只能输入数字的一种方法。外加回车换Tab (javascript key键的使用)+禁止切换输入法 
    第一步利用样式表。
    <asp:TextBox ID="TextBox1" runat="server" style="ime-mode:disabled" onkeydown="myKeyDown()"></asp:TextBox>第二步利用js脚本。
        <script type="text/javascript">
            function myKeyDown()
            {
             var   k=window.event.keyCode;            if ((k==46)||(k==8)||(k==189)||(k==109)||(k==190)||(k==110)|| (k>=48 && k<=57)||(k>=96 && k<=105)||(k>=37 && k<=40)) 
             {          }
             else if(k==13)
             {
                 window.event.keyCode = 9;
             }
             else
             {
                 window.event.returnValue = false;
             }
            }
        </script>利用样式表(style="ime-mode:disabled" )可以防止切换成汉字输入法,也就是说只能使用英文输入法,这样就防止了输入汉字的情况。js脚本就是要限制键盘输入,只能填入数字、小数点、负号、退格键、删除键和方向键。ascii码说明:
    8:退格键
    46:delete
    37-40: 方向键
    48-57:小键盘区的数字
    96-105:主键盘区的数字
    110、190:小键盘区和主键盘区的小数点
    189、109:小键盘区和主键盘区的负号13:回车
    9: Tab 就是那个把焦点移到下一个文本框的东东。对了还有两个缺点:
    1、没有验证多个小数点的情况。
    2、没有验证多个负号,和符号必须在前的情况。 http://88223100.cnblogs.com
      

  31.   

    采用tbox更新后则赋值为原来的值:
    页面里:
    <head runat="server">
        <SCRIPT language="javascript">
    var lastValue = "";//存储上一个的值
    //禁止输入非数字字符
    function numberTextChange(tbox)
    {
        
    if(!/^\d+$/.test(tbox.value) && tbox.value!="")//非数字,赋值为原来的值
    {
    tbox.value = lastValue
    }
    else//为数字则更新存储值
    {
    lastValue = tbox.value;
    }
    }
    </script>
    </head>后台代码里:protected void Page_Load(object sender, EventArgs e)
        {
            //增加脚本
            tbox.Attributes.Add("onpropertychange", "numberTextChange(this)");
            //输入法设置
            tbox.Style["ime-mode"] = "disabled";
        }
      

  32.   

    用脚本验证,在该控件的TEXTCHANGE事件中写代码就可以了
      

  33.   

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            If Len(TextBox1.Text) <= 1 Then
                If Not IsNumeric(TextBox1.Text) Then
                    TextBox1.Text = ""
                End If
            Else
                If Not IsNumeric(TextBox1.Text) Then
                    TextBox1.Text = Replace(TextBox1.Text, e.KeyChar, "")
                End If
            End If
        End Su
      

  34.   

    using System.Text.RegularExpressions;//添加。
    {
       public void chek_text(string strText)//添加一个方法
       {
          int   start=0;
          int   over=strText.lengh;
          int   lengh=strText.lengh-1;
          string str=@"[0-9]";
          Regex regex=new Regex(str);
          Match match_strText= Regex.Match(strText,start,over,lengh);
          bool  bool_str_Text=match_strText.success;
          
          while(bool_Text)
          {
             if(start==over)
             {
                 lengh--;
                 start++;
                 match match_strText=match_strText.nextMatch();
             }
            else
             {
               Console.WriteLine("完全匹配!");
               return;         }
          }
           Console.WriteLine("不完全匹配!"); 
         }
    }
        //在需要得地方加入方法调用
      chek_text(Textbox1.text);
      
      

  35.   

    IsNumeric(Text1.Text)  检查输入的是否为数字,如果是数字返回TRUE
      

  36.   

    js,或是.net自带得验证控件都行
      

  37.   


    <html>
    <head>
        <title>文本框限制</title>
    </head><script type="text/javascript">
    function regInput(reg)
    {
    var srcElem = event.srcElement;
    var oSel = document.selection.createRange();
    var srcRange = srcElem.createTextRange();
    oSel.setEndPoint("StartToStart", srcRange);
    var num = oSel.text + String.fromCharCode(event.keyCode) + srcRange.text.substr(oSel.text.length);
    event.returnValue = reg.test(num);
    }function fncKeyStop(evt)
    {
        if(!window.event)
        {
            var keycode = evt.keyCode; 
            var key = String.fromCharCode(keycode).toLowerCase();
            if(evt.ctrlKey && key == "v")
            {
              evt.preventDefault(); 
              evt.stopPropagation();
            }
        }
    }</script><body>
    只允许输入数字,且不能粘贴<br />
        <input type="text" onkeypress="regInput(/^[0-9]*$/)" onkeydown="fncKeyStop(event)" onpaste="return false" oncontextmenu = "return false;"/>
    </body>
    </html>