0

我有一个textBox,我希望它只接受0.00和之间的正小数20.00,我该怎么做?

4

4 回答 4

0

如果我理解你的问题。

   Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> "."c Then
            e.Handled = True
        End If

        'for ne decimal point
        If (e.KeyChar = "."c AndAlso (TextBox1).Text.IndexOf("."c) > -1) Then
            e.Handled = True
        End If
    End Sub

    Private Sub TextBox1_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
        If Decimal.Parse(TextBox1.Text) >= 0 AndAlso Decimal.Parse(TextBox1.Text) <= 20 Then
            'if positive decimals between 0.00 and 20.00
            'Do stuff
        Else
            'if not positive decimals between 0.00 and 20.00
            e.Cancel = True
        End If
    End Sub
于 2013-01-30T11:55:30.043 回答
0

If you need to make sure as the user is typing that only decimals are entered then apply an on key up event to it using javascript which checks the contents of the textbox, deleting any offending characters if any are present.

If you just need to check on submit, then do it within the code called on submit of the form.

于 2013-01-30T11:18:17.880 回答
0
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles TextBox1.TextChanged

    Dim myRegex As New Regex("^[0-9]*\.?[0-9]{0,2}$")
    If myRegex.IsMatch(TextBox1.Text.Trim) = False Then
        MsgBox("Invalid characters found")
        Exit Sub
    Else
        If CDec(TextBox1.Text.Trim) < 0 OrElse CDec(TextBox1.Text.Trim) > 20 Then
            MsgBox("Enter value between 0.00 and 20.00")
            Exit Sub
        End If
    End If

    End Sub
于 2013-01-31T07:33:40.870 回答
0

您可以TextBox像这样使用验证器

<asp:RangeValidator ID="range" runat="server" 
    ControlToValidate="text1" 
    MinimumValue="0" MaximumValue="20.99" Type="double" />

请查看这篇关于compareValidatorrangeValidatorregularExpressionValidator的文章了解更多详情

祝你好运

于 2013-01-30T11:34:06.147 回答