Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Home

15,099,838 members Sign in


home articles quick answers discussions features community help

 
Ask a Question All Questions All Unanswered FAQ

Validation for a text box with alphanumeric


4.00/5 (1 vote)
See more: C#

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
Top Experts
Thanks Last 24hrs This month
OriginalGriff 280 OriginalGriff 2,883
Regards
CHill60 100 Richard MacCutc… 1,728

Posted 14-May-14 23:53pm Richard MacCutc… 50 Richard Deeming 880


afsal.mp
CPallini 20 CHill60 525

Alex Dunlop 10 Dave Kreskowiak 410


Add a Solution

Related Questions

5 solutions Top Rated Most Recent


Text box validation with alphanumerics with
some specific special characters

Regular Extression Validation for Alphanumeric


Solution 1 Values

Alphanumeric text with dot validation


If you don't want to allow any other characters entry except for the alphanumeric characters in a TextBox, then How to extract all alphabet from a string
you can do this on the KeyPress event of a TextBox. containing alphanumeric in VB.NET
In the KeyPress Event, you need to check whether the entered character is either a Letter or a Digit.
Text Box Validation
Char.IsLetterOrDigit(e.KeyChar) Text Box Validation
If yes, then allow the Key pressing by setting
"e.Handled = false" How to set numeric, alphanumeric property to
text box
Otherwise, don't allow the Key pressing by setting "e.Handled = true"
Text Box Validation
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 Text box validations
to do that, you need to add one more condition in the if block as
alphanumeric validation in javascript

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;
}
}

Posted 14-May-14 23:59pm


sankarsan parida

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)

Solution 2
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;
}

Posted 15-May-14 0:11am


_Asif_

Solution 3
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");
}
}
}

Posted 15-May-14 0:56am


Peter Leow

Solution 4
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.

Posted 15-May-14 4:22am Updated 15-May-14 4:32am v2


Marcin Kozub

Solution 5
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+$");
}

Posted 15-May-14 4:43am


CHill60

Add a Solution

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

Advertise Layout: fixed | fluid Copyright © CodeProject, 1999-2021


Privacy All Rights Reserved.
Cookies
Terms of Use Web02 2.8.20211110.1
Last Updated 15 May 2014

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

You might also like