文本框中仅允许数字和句号(句号)

发布于 2024-10-18 01:25:05 字数 356 浏览 1 评论 0原文

我知道以下代码阻止用户在文本框中使用空格,但是我如何允许用户仅使用数字和 fulstop(这样我可以添加像 1.5 这样的值)

    Private Sub Textbox4_keyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox4.KeyDown

    If e.KeyCode = Keys.Space Then
        TextBox4.Clear()
        MsgBox("Invalid character. No spaces Permited...")

    End If

I know that the following code blocks the usser from ussing spaces in a textbox however how do i allow the user to only use numbbers and a fulstop (so i can add values like 1.5)

    Private Sub Textbox4_keyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox4.KeyDown

    If e.KeyCode = Keys.Space Then
        TextBox4.Clear()
        MsgBox("Invalid character. No spaces Permited...")

    End If

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆悲凉 2024-10-25 01:25:05

从可用性的角度来看,在 KeyDown 事件中测试有效输入并不好。例如,当用户想要将文本粘贴到文本框中时会发生什么?

此外,用户仍然可以使用文本框的上下文菜单粘贴无效输入,您的代码不会注意到这一点。

您应该允许所有输入,然后在用户离开文本框时测试有效性。 VB 为此有一个额外的事件,每当文本框失去焦点时就会触发该事件:Validating。使用此事件(并且此事件)来测试有效输入。

From a usability point of view, testing for valid input in the KeyDown event isn’t good. For example, what happens when the user wants to paste text into your text box?

Furthmore, the user can still paste invalid input using the TextBox’ context menu, your code won’t notice this.

You should allow all input, and then test for validity when the user leaves the text box. VB has an extra event for this, which is fired whenever the text box loses focus: Validating. Use this event (and only this event) to test for valid input.

黯然#的苍凉 2024-10-25 01:25:05

一种简单的方法可能是查找“允许的”字符,如果不是其中之一,则显示错误消息。

A simple approach for this might be to look for the "allowed" characters, if not one of them, show the error message.

荆棘i 2024-10-25 01:25:05

在过去 20 年编写代码的过程中,我始终使用以下文本框检查字符的基本原理。
首先,您必须创建一个单独的类,您可以将其命名为(为了方便起见)Char_Validation。
在这个类中,您将放置一个返回布尔值的函数,如下所示。

Public Class Char_Validation
    Public Const Gr As String = "Greek"
    Public Const En As String = "English"
    Public Const Num As String = "Numbers"
    Public Const FullGr As String = "Full Greek"
    Public Const FullEn As String = "Full English"
    Public Const EnN As String = "English with Numbers"
    Public Const GrN As String = "Greek with Numbers"

    Public Shared Function ValidateChar(ByVal AsciiChar As String, ByVal CharTable As String, ByVal sender As Object, ByVal e As System.EventArgs) As Boolean
        Dim ConvChar As Integer = CUInt(Microsoft.VisualBasic.Asc(AsciiChar))
        Dim ConvCharW As Integer = CUInt(Microsoft.VisualBasic.AscW(AsciiChar))

        ValidateChar = False

        Select Case CharTable
            Case En
                Select Case ConvChar
                    Case 65 To 126, 145 To 150, 8, 32 To 47, 58 To 64, 128, 130
                        ValidateChar = True
                End Select
            Case EnN
                Select Case ConvChar
                    Case 48 To 57, 65 To 126, 8, 32, 45
                        ValidateChar = True
                End Select
.
.
.
.
.
            Case Num
                Select Case ConvChar
                    Case 44 To 57, 92, 8
                        ValidateChar = True
                End Select
        End Select

    End Function
End Class

在 Form 中的类中,您将使用 TextBox_KeyPress,并在其中使用以下代码。

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        ErrorProvider1.Clear()
        ErrorLabel.ForeColor = Drawing.Color.Black
    Select Case Char_Validation.ValidateChar(e.KeyChar, Char_Validation.Num, sender, e)
            Case True
            Case False
                ErrorProvider1.SetError(TextBox1, "Wrong Character Only Numbers")
                Beep()
    e.KeyChar = ""
        End Select
    End Sub

因此,您将禁止用户将字符置于您的决定之外。
我希望从现在起这将涵盖您。

In last 20years of writing code I always use the following rationale for TextBoxes Check Characters.
First you have to create a separate Class which you may call it (for your convenience) Char_Validation.
Inside to this Class you’ll put a Function which returns Boolean as follows .

Public Class Char_Validation
    Public Const Gr As String = "Greek"
    Public Const En As String = "English"
    Public Const Num As String = "Numbers"
    Public Const FullGr As String = "Full Greek"
    Public Const FullEn As String = "Full English"
    Public Const EnN As String = "English with Numbers"
    Public Const GrN As String = "Greek with Numbers"

    Public Shared Function ValidateChar(ByVal AsciiChar As String, ByVal CharTable As String, ByVal sender As Object, ByVal e As System.EventArgs) As Boolean
        Dim ConvChar As Integer = CUInt(Microsoft.VisualBasic.Asc(AsciiChar))
        Dim ConvCharW As Integer = CUInt(Microsoft.VisualBasic.AscW(AsciiChar))

        ValidateChar = False

        Select Case CharTable
            Case En
                Select Case ConvChar
                    Case 65 To 126, 145 To 150, 8, 32 To 47, 58 To 64, 128, 130
                        ValidateChar = True
                End Select
            Case EnN
                Select Case ConvChar
                    Case 48 To 57, 65 To 126, 8, 32, 45
                        ValidateChar = True
                End Select
.
.
.
.
.
            Case Num
                Select Case ConvChar
                    Case 44 To 57, 92, 8
                        ValidateChar = True
                End Select
        End Select

    End Function
End Class

At your Class in Form you will use the TextBox_KeyPress on which you’ll use the following code.

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        ErrorProvider1.Clear()
        ErrorLabel.ForeColor = Drawing.Color.Black
    Select Case Char_Validation.ValidateChar(e.KeyChar, Char_Validation.Num, sender, e)
            Case True
            Case False
                ErrorProvider1.SetError(TextBox1, "Wrong Character Only Numbers")
                Beep()
    e.KeyChar = ""
        End Select
    End Sub

Thus you will prohibit the user to place characters out of your decision.
I hope that will cover you from now.

别在捏我脸啦 2024-10-25 01:25:05

以下代码适用于我:firefox、IE 8、chrome、Safari 和 iphone。

函数点放置(myfield){
if(myfield.indexOf(".")==-1){
返回假;
}
返回真;
}
函数 NumbersOnly(myfield, e) {
var 键;
var keychar;

if (window.event) {
    key = window.event.keyCode;
}
else if (e) {
    key = e.which;
}
else {
    return true;
}

keychar = String.fromCharCode(key);

// control keys
if ((key == null) || (key == 0) || (key == 8) ||
(key == 9) || (key == 13) || (key == 27)) {
    return true;
}
// numbers
else if ((("0123456789").indexOf(keychar) > -1)) {
    return true;
}
// decimal point jump
else if (!dotplaced(myfield.value) && (keychar == ".")) {
    //myfield.form.elements[dec].focus();        
    return true;
}
else {
    return false;
}

}

Following code worked for me on : firefox, IE 8, chrome, Safari and iphone.

function dotplaced(myfield){
if(myfield.indexOf(".")===-1){
return false;
}
return true;
}
function NumbersOnly(myfield, e) {
var key;
var keychar;

if (window.event) {
    key = window.event.keyCode;
}
else if (e) {
    key = e.which;
}
else {
    return true;
}

keychar = String.fromCharCode(key);

// control keys
if ((key == null) || (key == 0) || (key == 8) ||
(key == 9) || (key == 13) || (key == 27)) {
    return true;
}
// numbers
else if ((("0123456789").indexOf(keychar) > -1)) {
    return true;
}
// decimal point jump
else if (!dotplaced(myfield.value) && (keychar == ".")) {
    //myfield.form.elements[dec].focus();        
    return true;
}
else {
    return false;
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文