验证:如何从文本框中删除特殊字符? [英] Validation:How to remove special characters from textbox?

查看:108
本文介绍了验证:如何从文本框中删除特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!全部,
我有一个应用程序,我必须在文本框中输入一些字符,如何验证它,以便文本框在文本框的开头(包括空格)不接受特殊字符.

Hi! all,
I have an application where i have to enter some characters in a textbox, how can i validate it so that the textbox doesn''t accept special characters in the beginning of the textbox including a space. It should only allow to enter Characters & Numeric only.

推荐答案

您可以使用TextChanged事件,并且如果最后添加的字符不在可接受的字符列表中,立即将其从文本框中删除.请注意插入符号的位置,因为当您更改文本内容时,插入符号会重新设置为该字段的开头.
You could use the TextChanged event and if the last character added isn''t in a list of acceptable characters, delete it right away from the textbox. be mindful of the caret position though because when you change the text contents, the caret gets reset back tot he beginning of the field.


KeyPress事件可能会错过用户粘贴时的情况只需使用鼠标即可将一些内容输入文本框.在我看来,约翰建议使用TextChanged是一个更好的选择.另外,您可能需要研究正则表达式.

此处 [ Google [
The KeyPress Event could miss scenarios such as when a user pastes something into a textbox using only the mouse. In my opinion, John''s suggesion of using the TextChanged is a better event to use. Also, you may want to research Regular Expressions.

Here[^] is a C# article that could help give you an idea of how to use them.

Or here are some Google[^] links that could help.


尝试此操作可能会对您有所帮助
内部类声明此功能:

Try this may this help you
Inside Class Declare this Function:

Public Sub txtValidation(ByVal e)
        If (Asc(e.KeyChar)) < 65 Or (Asc(e.KeyChar)) > 90 And (Asc(e.KeyChar)) < 97 Or (Asc(e.KeyChar)) > 122 Then

            If (Asc(e.KeyChar) <> 32) Then
                e.Handled = True
            End If
        End If
        If (Asc(e.KeyChar) = 8) Then
            e.Handled = False
        End If
    End Sub


上面的代码可以避免输入特殊字符,但是只能输入字母,下面给出的功能可以避免输入特殊字符,但是只能输入数字


The above will avoid you to enter Special Character but allow you to Input Alphabets Only.And the Below given function will avoid you to enter Special Characters but allow you to Enter Numbers only

Public Sub txtNumberValidation(ByVal e)
        If (Asc(e.KeyChar) < 48) Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
            e.Handled = True

        End If
        If (Asc(e.KeyChar) = 8) Then
            e.Handled = False
        End If
    End Sub


如果遇到问题,请在您的TextBoxes的Keypress事件中从类调用此函数到窗体
此链接[
^ ]

我已经使用ASCII码来检测输入的值.对其进行研究,您肯定会获得所需的解决方案.尝试一下


Call this Function from your Class to your Form In the Keypress event of your TextBoxes if having Problem follow
this Link [^]

I have used ASCII codes to detect the Entered Value.Research on it You Will definitely get your Required Solution.Try it


这篇关于验证:如何从文本框中删除特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆