Click here to Skip to main content
13,056,238 members (84,312 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I want to restrict my user to strictly input alphanumeric password only to open my application. Please provide c# code or algorithm for the same.
Posted 31-Jan-11 23:23pm
sk saini7.8K
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Register Textbox KeyPressEvent
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
// Allows only Alpha-Numeric’s
if (!(Char.IsLetter(e.KeyChar) || Char.IsDigit(e.KeyChar) || Char.IsControl (e.KeyChar)))
e.Handled = true;
sk saini 1-Feb-11 6:45am
Thanks, its working.
shakil0304003 1-Feb-11 6:48am
Pravin Patil, Mumbai 1-Feb-11 10:12am
Great solution shakil..
Simple but effective.
SAKryukov 1-Feb-11 15:06pm
Correct; I voted "5". However the purpose is completely wrong, I think.
Please see my answer.
Marcus Kramer 1-Feb-11 15:28pm
Agreed. +5 because the answer is 100% for the OP's question, but I too have serious questions about the theory behind doing this.
SAKryukov 1-Feb-11 16:15pm
That's why I voted for Pravin's answer first.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Refer Password Strength Control[^]

It has an excellent code to measure password strength.
I hope this helps.

All the best.
SAKryukov 1-Feb-11 14:59pm
You're offering just the opposite to what OP wants; and I totally agree with you! - my 5.
I referenced your answer in mine -- please see.
Pravin Patil, Mumbai 2-Feb-11 0:53am
Thanks SA.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Just a note: what you want is considered as unsafe practice even in user's preferences. Look at any article on password strength. Pravin's answer is probably very adequate.

While other works tries to offer ways of encouraging users to create stronger passwords or, very typically, limits passwords to stronger ones by denying simple ones, you do exactly the opposite!

This is up to you, of course, but you're not helping your customers at all. It would be much, much, way to much better to allow them using any passwords at all.

Thank you for your understanding.

SAKryukov 2-Feb-11 3:16am
I would say, this vote of "3" is a bright manifestation of stupidity, inability to understand a good advice, it it is contradict the preoccupation, no matter how wrong.
Sorry, for you, loser!
Manas Bhardwaj 2-Feb-11 4:02am
+5 SA.
SAKryukov 2-Feb-11 4:08am
Thank you, Manas.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

In the TextBox Event Select TextChainged Event And then place this code there
Note:-Replace Textbox Name Here
string charactersAllowed = "0123456789";
string theText = TEXTBOXNAME.Text;
string Letter = null;
int SelectionIndex = TEXTBOXNAME.SelectionStart;
int Change = 0;
for (int x = 0; x <= TEXTBOXNAME.Text.Length - 1; x++) {
    Letter = TEXTBOXNAME.Text.Substring(x, 1);
    if (charactersAllowed.Contains(Letter) == false) {
        theText = theText.Replace(Letter, string.Empty);
        Change = 1;
TEXTBOXNAME.Text = theText;
TEXTBOXNAME.Select(SelectionIndex - Change, 0);

CHECK OUT This it Realy Works.....
All the Best ....
sk saini 1-Feb-11 6:24am
I applied this code but it doesn't accepts any letter in Textbox.
sk saini 1-Feb-11 6:25am
Actually its taking input letter but simultaneously deleting the same.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170728.2 | Last Updated 1 Feb 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100