Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in c#.net,for a textbox,it should accept all numbers,alphabets,special characters but when i give numbers alone it should throw error as"enter alphabets also"...how to code...can anyone help me.please..
Posted
Updated 28-Dec-12 5:50am
v2

Option 1: Use C# Regex class[^]. The codition to check: at least one non-digit character. So, the acceptance pattern could be @"\D". E.g.
C#
bool IsOk(s) { return Regex.IsMatch(s, @"\D"); }


Option 2: loop over all characters of the string an check with char.IsDigit[^] method if at least one character is not a digit, e.g.
C#
bool IsOk(string s) { return s.Length > 0 && s.Any(c=>!char.IsDigit(c));


Cheers
Andi
 
Share this answer
 
v2
Comments
nityasri 28-Dec-12 12:08pm    
@andreas thanks
Andreas Gieriet 28-Dec-12 14:48pm    
You are welcome.
Andi
PS: Please mark if any of the solutions solve your problem.
 
Share this answer
 
Comments
nityasri 28-Dec-12 11:53am    
@krunal..thanks
[no name] 28-Dec-12 11:54am    
If you're satisfied with the given answer, Accept the Solution :)
-Krunal R.

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