掩蔽接受正小数

时间:2013-01-30 11:12:25

标签: vb.net winforms textbox

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

4 个答案:

答案 0 :(得分:0)

如果您需要确保在用户键入时仅输入小数,则使用javascript检查文本框的内容,删除任何有问题的字符(如果存在)。

如果您只需要检查提交,请在提交表单的代码中进行。

答案 1 :(得分:0)

您可以在TextBox上使用验证码

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

请查看有关compareValidatorrangeValidatorregularExpressionValidator的文章,了解更多详情

祝你好运

答案 2 :(得分: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

答案 3 :(得分: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