By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,785 Members | 2,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,785 IT Pros & Developers. It's quick & easy.

validating textbox

P: n/a
Hi Guys,

I have a textbox in windows form that should only accept alphabets,
numbers, spaces and underscore. If the textbox contains anyother
character it should display a msg at the time of validation.. Is there
any funciton in vb.net for this? or any other way??
Waiting for ur replies...

Thanks in adv.

Aug 28 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
There are at least couple of ways to do this: 1. Filtering the charaters in
the Keypress event of the textbox itself 2. Using regular expressions to
find invalid character in the textbox. See which one works ut for you...

<am************@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Hi Guys,

I have a textbox in windows form that should only accept alphabets,
numbers, spaces and underscore. If the textbox contains anyother
character it should display a msg at the time of validation.. Is there
any funciton in vb.net for this? or any other way??
Waiting for ur replies...

Thanks in adv.

Aug 28 '06 #2

P: n/a
Using the KeyPress event does not preclude the user from cutting from some
other source and pasting invalid chars into the text box. Use the textbox's
"Validating" event to check the text for non-alpha characters.
--
Dennis in Houston
"Siva M" wrote:
There are at least couple of ways to do this: 1. Filtering the charaters in
the Keypress event of the textbox itself 2. Using regular expressions to
find invalid character in the textbox. See which one works ut for you...

<am************@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Hi Guys,

I have a textbox in windows form that should only accept alphabets,
numbers, spaces and underscore. If the textbox contains anyother
character it should display a msg at the time of validation.. Is there
any funciton in vb.net for this? or any other way??
Waiting for ur replies...

Thanks in adv.

Aug 28 '06 #3

P: n/a
If you want to prevent illegal characters from being pasted you can use this
code:

Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As
Boolean
'Need to prevent pasting of invalid characters
If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData =
(Keys.Control Or Keys.V) Then
Dim data As IDataObject = Clipboard.GetDataObject
If data Is Nothing Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
Dim text As String =
CStr(data.GetData(DataFormats.StringFormat, True))
If text = String.Empty Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
For Each ch As Char In text.ToCharArray
If Not IsValid(ch) Then
Return True
End If
Next
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End If
Else
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Function

Private Function IsValid(ByVal ch As Char) As Boolean
'TODO: Check if the character is valid
End Function

The above will prevent pasting if the text contains any illegal characters.

/claes

"Dennis" <De****@discussions.microsoft.comwrote in message
news:DC**********************************@microsof t.com...
Using the KeyPress event does not preclude the user from cutting from some
other source and pasting invalid chars into the text box. Use the
textbox's
"Validating" event to check the text for non-alpha characters.
--
Dennis in Houston
"Siva M" wrote:
>There are at least couple of ways to do this: 1. Filtering the charaters
in
the Keypress event of the textbox itself 2. Using regular expressions to
find invalid character in the textbox. See which one works ut for you...

<am************@gmail.comwrote in message
news:11**********************@i42g2000cwa.googleg roups.com...
Hi Guys,

I have a textbox in windows form that should only accept alphabets,
numbers, spaces and underscore. If the textbox contains anyother
character it should display a msg at the time of validation.. Is there
any funciton in vb.net for this? or any other way??
Waiting for ur replies...

Thanks in adv.


Aug 29 '06 #4

P: n/a
I believe you have to subclass the textbox to override the ProcessCmdKey
whereas with the validating event you don't have to do any subclassing. it's
easier to work with at design time.
--
Dennis in Houston
"Claes Bergefall" wrote:
If you want to prevent illegal characters from being pasted you can use this
code:

Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As
Boolean
'Need to prevent pasting of invalid characters
If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData =
(Keys.Control Or Keys.V) Then
Dim data As IDataObject = Clipboard.GetDataObject
If data Is Nothing Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
Dim text As String =
CStr(data.GetData(DataFormats.StringFormat, True))
If text = String.Empty Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
For Each ch As Char In text.ToCharArray
If Not IsValid(ch) Then
Return True
End If
Next
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End If
Else
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Function

Private Function IsValid(ByVal ch As Char) As Boolean
'TODO: Check if the character is valid
End Function

The above will prevent pasting if the text contains any illegal characters.

/claes

"Dennis" <De****@discussions.microsoft.comwrote in message
news:DC**********************************@microsof t.com...
Using the KeyPress event does not preclude the user from cutting from some
other source and pasting invalid chars into the text box. Use the
textbox's
"Validating" event to check the text for non-alpha characters.
--
Dennis in Houston
"Siva M" wrote:
There are at least couple of ways to do this: 1. Filtering the charaters
in
the Keypress event of the textbox itself 2. Using regular expressions to
find invalid character in the textbox. See which one works ut for you...

<am************@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Hi Guys,

I have a textbox in windows form that should only accept alphabets,
numbers, spaces and underscore. If the textbox contains anyother
character it should display a msg at the time of validation.. Is there
any funciton in vb.net for this? or any other way??
Waiting for ur replies...

Thanks in adv.



Aug 30 '06 #5

P: n/a
You're correct. The code below requires an override. If it is enough to
validate when they leave the field then using the Validating event is
easier. I just wanted to point out another option.

/claes

"Dennis" <De****@discussions.microsoft.comwrote in message
news:72**********************************@microsof t.com...
>I believe you have to subclass the textbox to override the ProcessCmdKey
whereas with the validating event you don't have to do any subclassing.
it's
easier to work with at design time.
--
Dennis in Houston
"Claes Bergefall" wrote:
>If you want to prevent illegal characters from being pasted you can use
this
code:

Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys)
As
Boolean
'Need to prevent pasting of invalid characters
If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData =
(Keys.Control Or Keys.V) Then
Dim data As IDataObject = Clipboard.GetDataObject
If data Is Nothing Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
Dim text As String =
CStr(data.GetData(DataFormats.StringFormat, True))
If text = String.Empty Then
Return MyBase.ProcessCmdKey(msg, keyData)
Else
For Each ch As Char In text.ToCharArray
If Not IsValid(ch) Then
Return True
End If
Next
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End If
Else
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Function

Private Function IsValid(ByVal ch As Char) As Boolean
'TODO: Check if the character is valid
End Function

The above will prevent pasting if the text contains any illegal
characters.

/claes

"Dennis" <De****@discussions.microsoft.comwrote in message
news:DC**********************************@microso ft.com...
Using the KeyPress event does not preclude the user from cutting from
some
other source and pasting invalid chars into the text box. Use the
textbox's
"Validating" event to check the text for non-alpha characters.
--
Dennis in Houston
"Siva M" wrote:

There are at least couple of ways to do this: 1. Filtering the
charaters
in
the Keypress event of the textbox itself 2. Using regular expressions
to
find invalid character in the textbox. See which one works ut for
you...

<am************@gmail.comwrote in message
news:11**********************@i42g2000cwa.googleg roups.com...
Hi Guys,

I have a textbox in windows form that should only accept alphabets,
numbers, spaces and underscore. If the textbox contains anyother
character it should display a msg at the time of validation.. Is there
any funciton in vb.net for this? or any other way??
Waiting for ur replies...

Thanks in adv.




Aug 31 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.