Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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;
}
 
Share this answer
 
Comments
sk saini 1-Feb-11 6:45am    
Thanks, its working.
shakil0304003 1-Feb-11 6:48am    
Welcome.
Pravin Patil, Mumbai 1-Feb-11 10:12am    
Great solution shakil..
Simple but effective.
Sergey Alexandrovich Kryukov 1-Feb-11 15:06pm    
Correct; I voted "5". However the purpose is completely wrong, I think.
Please see my answer.
Best,
--SA
fjdiewornncalwe 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.
Refer Password Strength Control[^]

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

All the best.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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.
--SA
Pravin Patil, Mumbai 2-Feb-11 0:53am    
Thanks SA.
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.

—SA
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 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!
--SA
Manas Bhardwaj 2-Feb-11 4:02am    
+5 SA.
Sergey Alexandrovich Kryukov 2-Feb-11 4:08am    
Thank you, Manas.
--SA
In the TextBox Event Select TextChainged Event And then place this code there
Note:-Replace Textbox Name Here
C#
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 ....
 
Share this answer
 
Comments
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)



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