473,320 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

validating textbox

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
5 4777
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alex Bink | last post by:
Hi, I have a validating event on a textbox in which I want to prevent the user to leave the textbox without entering the right data. Only if he clicks on another specific control he is allowed...
2
by: Chris Dunaway | last post by:
I have a form with a textbox and numerous panels, buttons and other controls. I have handled the textbox Validating and Validated events. The textbox will hold a filename. In the validating...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
7
by: Bruce HS | last post by:
I'd like to call my ancestor Validation Function every time any control on a Win Form generates a Validating or Validated event. I'm using VB. I've extended Textbox, for instance, to have its...
6
by: Ryan | last post by:
I have a windows form that I want to force validation on controls (text boxes) when the user clicks a "Save" button. The only way I've found to do this is to cycle through every control and call...
3
by: TheSteph | last post by:
Hi Experts ! I have a Winform Program in C# / .NET 2.0 I would like to ensure that a value in a TextBox is a valid Int32 when user get out of it (TextBox loose focus)
1
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I've noticed that controls do not raise a Validating event if they are contained in a ToolStripDropDown via a ToolStripControlHost item. Please run the following sample and follow the instructions...
8
by: Peted | last post by:
I have an amazing problem which i think i have no hope of solving Im working with a c# dot net module that is hosted by and runs under a delphi form envrioment. Dont ask me how this insanity has...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.