Click here to Skip to main content
15,913,669 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

I am new to C#. I need a validation for a text box. Text box may contain alphanumeric but only numbers is not allowed. How can i do that. It is in C# windows application.

Example : 12Abc --------- Allowed
: 12546---------- Not allow
: Asd23lk-------- Allow


Thanks

Regards
Posted

If you don't want to allow any other characters entry except for the alphanumeric characters in a TextBox, then you can do this on the KeyPress event of a TextBox.
In the KeyPress Event, you need to check whether the entered character is either a Letter or a Digit.

Char.IsLetterOrDigit(e.KeyChar)
If yes, then allow the Key pressing by setting
"e.Handled = false"

Otherwise, don't allow the Key pressing by setting "e.Handled = true"
In addition to the alphanumeric characters, generally one should allow the backspace character too along with the alphanumeric characters so that the user can remove any wrong character if he entered by mistake. In order to do that, you need to add one more condition in the if block as
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
    if (Char.IsLetterOrDigit(e.KeyChar)     // Allowing only any letter OR Digit
            ||e.KeyChar == '\b')                 // Allowing BackSpace character
    {
        e.Handled = false;
    }
    else
    {
        e.Handled = true;
    }
} 
 
Share this answer
 
Comments
afsal.mp 15-May-14 5:06am    
If we are inputting fully numbers. It is not allowed.
[no name] 15-May-14 5:12am    
Yes it will alow for numeric and alpha characters also.
CHill60 15-May-14 9:39am    
4'd ... good call on the backspace character, but doesn't satisfy full requirement (all numbers not allowed)
Try this

C#
public bool IsAlphaNumeric(string inputString)
{
Regex reg = new Regex("^(\d)*+[A-Za-z]+(\d)*+.*$");
if (reg.IsMatch(inputString))
return true;
else
return false;
}
 
Share this answer
 
Try Regex.Match, see example:
C#
using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string text = "aaa2222aaaa";

        Match match = Regex.Match(text, @"([a-zA-Z]\w+)|(\w+[a-zA-Z])",
            RegexOptions.IgnoreCase);

        if (match.Success)
        {
            Console.WriteLine("passed");
        }
        else
        {
            Console.WriteLine("failed");
        }
    }
}
 
Share this answer
 
In addition to Solution 1 (sankarsan parida) which allows you to enter only letters and digits in you text box control, you can check that textbox is containing at least one letter this way:

C#
var isValid = textBox1.Text.ToCharArray().Any(x => Char.IsLetter(x));


Hope it help you.
 
Share this answer
 
v2
Some very valid solutions given (in particular from Sankarsan Parida), but no-one has mentioned where to put the check that the text is not all numbers.

The following should work for both only allowing alphanumeric to be entered and for checking it is not all numeric (once the user has finished entering data) ...

C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    //"handle" the key press if is not alphanumeric or backspace
    //because it is handled here it won't appear in the textBox
    e.Handled = !(Char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == '\b');
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
    //Cancel the exit from the textBox if it is all numeric
    e.Cancel = Regex.IsMatch(textBox1.Text, @"^\d+$");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900