Click here to Skip to main content
15,909,437 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

In my program, there is a textbox asking the user to create a Username for himself. The Username should consist of digits or letters only. I mean, @, &, # or ... should be excluded. How can I detect that the entered Username just includes these legal (digits or letters) entries.

Thanks a lot
Posted

one way to do that is using regular expressions ...

Regex regex = new Regex("^[A-Za-z0-9]+$");
            if (regex.IsMatch(textBox1.Text))
                MessageLabel.Text= "valid";
            else
                MessageLabel.Text= "Not Valid";


just off the top of my head ...
 
Share this answer
 
Comments
ahhashemi 12-Aug-10 15:25pm    
Reason for my vote of 5
Dear samqty

Your answer was exactly what I was looking for and hit the target. Thanks a lot and Good luck.
If you don't want to use Regex you can loop through each character in the string and test it with char.IsLetterOrDigit[^]
 
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