Alphanumeric character validation in C#

  • Thread starter Thread starter kanepart2
  • Start date Start date
K

kanepart2

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
 
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.
 
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;
}
}
 
//
// 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.
 
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.
 
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/library/system.windows.forms.keyeventargs.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
 
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.)
 
Can we use this c# code to encrypt alphanumeric plaintext in RSA algorithm?
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
On Wednesday, May 30, 2007 1:31 PM Mahmoud Al-Qudsi wrote:
try isDigit and isChar functions when validating the .keydown action.
On Wednesday, May 30, 2007 1:59 PM Morten Wennevik [C# MVP] wrote:
Even better,

Char.IsLetterOrDigit(char), you need to use the KeyPress event to get the characters and cancel (Handled) the event.
 
Can we use alphanumeric plaintext in c# using RSA algorithm?
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
On Wednesday, May 30, 2007 1:31 PM Mahmoud Al-Qudsi wrote:
try isDigit and isChar functions when validating the .keydown action.
On Wednesday, May 30, 2007 1:59 PM Morten Wennevik [C# MVP] wrote:
Even better,

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