472,141 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Alphanumeric character validation in C#

Hey all,

I have to validate a textbox in windows forms for alphanumeric
characters such that non alphanumeric key presses are ignored.

Some help would be appreciated

May 30 '07 #1
7 21120
On May 30, 8:15 pm, kanepa...@hotmail.com wrote:
Hey all,

I have to validate a textbox in windows forms for alphanumeric
characters such that non alphanumeric key presses are ignored.

Some help would be appreciated
try isDigit and isChar functions when validating the .keydown action.

May 30 '07 #2

kanepa...@hotmail.com wrote:
Hey all,

I have to validate a textbox in windows forms for alphanumeric
characters such that non alphanumeric key presses are ignored.

Some help would be appreciated

This is not complete but something like this I gues:

You can add KeyDown event handler to your textbox ... for example:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.OemMinus) // and of course more checks
here ....
{
e.SuppressKeyPress = true;
e.Handled = true;
}
}

May 30 '07 #3
//
// AlphaNumTextBox.cs
//

using System;
using System.Windows.Forms;

namespace AlphaNumericTextBox
{
/// <remarks>
/// A text box to contain alphanumeric data
/// </remarks>
internal sealed class AlphaNumTextBox : TextBox
{
#region Fields

private bool isBack = false;
private bool isAlphaNum = false;

#endregion

#region Event raisers

protected override void OnKeyDown(KeyEventArgs e)
{
Keys k = e.KeyCode;

if (k == Keys.Back)
{
this.isBack = true;
this.isAlphaNum = false;
}
else
{
bool isAlpha = !e.Alt && !e.Control && (k >= Keys.A && k <=
Keys.Z);

// Determine whether the keystroke is a number
bool d0d9 = k >= Keys.D0 && k <= Keys.D9;
bool numpad = k >= Keys.NumPad0 && k <= Keys.NumPad9;
bool isNumber = (!e.Alt && !e.Control && !e.Shift) && (d0d9
|| numpad);

this.isBack = false;
this.isAlphaNum = isAlpha || isNumber;
}
base.OnKeyDown(e);
}

protected override void OnKeyPress(KeyPressEventArgs e)
{
if (!this.isBack && !this.isAlphaNum)
{
e.Handled = true;
return;
}

char c = e.KeyChar;

if (this.isBack && this.SelectionStart 0)
{
int currentPosition = SelectionStart;
this.Text = this.Text.Substring(0, this.TextLength - 1);
this.SelectionStart = currentPosition - 1;
e.Handled = true;
return;
}

if (this.isAlphaNum && this.SelectionStart < this.MaxLength)
{
int currentPosition = SelectionStart;
this.Text = this.Text.Insert(currentPosition,
Char.ToUpper(c).ToString());
this.SelectionStart = currentPosition + 1;
e.Handled = true;
return;
}

base.OnKeyPress(e);
}

#endregion
}
}
Bye,
Luigi.
May 30 '07 #4
On May 30, 1:31 pm, Daniel Cigic <daniel.ci...@googlemail.comwrote:
kanepa...@hotmail.com wrote:
Hey all,
I have to validate a textbox in windows forms for alphanumeric
characters such that non alphanumeric key presses are ignored.
Some help would be appreciated
I tried both the above . e does not have a property called KeyCode
This is not complete but something like this I gues:

You can add KeyDown event handler to your textbox ... for example:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.OemMinus) // and of course more checks
here ....
{
e.SuppressKeyPress = true;
e.Handled = true;
}
}

May 30 '07 #5
On Wed, 30 May 2007 19:31:01 +0200, Mahmoud Al-Qudsi <mq****@gmail.comwrote:
On May 30, 8:15 pm, kanepa...@hotmail.com wrote:
>Hey all,

I have to validate a textbox in windows forms for alphanumeric
characters such that non alphanumeric key presses are ignored.

Some help would be appreciated

try isDigit and isChar functions when validating the .keydown action.

Even better,

Char.IsLetterOrDigit(char), you need to use the KeyPress event to get the characters and cancel (Handled) the event.

--
Happy coding!
Morten Wennevik [C# MVP]
May 30 '07 #6
On Wed, 30 May 2007 10:51:07 -0700, <ka*******@hotmail.comwrote:
I tried both the above . e does not have a property called KeyCode
Sure it does, if you are actually handling the KeyDown event which has the
KeyEventArgs parameter:

http://msdn2.microsoft.com/en-us/lib...s.keycode.aspx

That said, it seems to me that Morton's advice about using the KeyPress
event instead is better. Set the e.Handled property to "true" to avoid
non-alphanumeric characters from being entered.

Also note that there's a MaskedTextBox that will do this sort of filtering
for you. If all you are trying to do is restrict input to alphanumerics,
that might be a better choice.

Pete
May 30 '07 #7
Hey all,
>
I have to validate a textbox in windows forms for alphanumeric
characters such that non alphanumeric key presses are ignored.
Filtering at key-press is usually the bad approach.
It will mess-up a languages that have more complex input
(dead-keys, IME, etc.)
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
May 31 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Shozi | last post: by
2 posts views Thread by VB Programmer | last post: by
2 posts views Thread by joe.minga | last post: by
3 posts views Thread by pradeep | last post: by
kaleeswaran
11 posts views Thread by kaleeswaran | last post: by

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.