Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want regular expression that can accept numbers and alphabets or just alphabets but not numbers only.

I am new to this expressions, any one suggest me the expression.

valid:

alphabets + numerics

alphabets only

numerics + alphabets


Invalid:

0-9(numerics only)
Posted
Updated 5-Jan-18 0:38am

Hi,
Try following regex
^\d*[a-zA-Z]{1,}\d*


This will be valid if string has atleast 1 alphabet and any number of digit.

I tried above regex using C# code I shared in the comment for following string
abc : Valid,
abc1: Valid,
1abc: Valid,
111: Invalid
a1b1c1: Valid


Thanks
Milind
 
Share this answer
 
v3
Comments
[no name] 8-Nov-12 3:13am    
If the string is like:

123abc ,is treated as invalid , but it can be allowed.
MT_ 8-Nov-12 3:19am    
I tried this
string testing = "123abc";//also tried 123,abc,1abc etc
Regex regex = new Regex(@"[a-zA-Z]{1,}\d*");
Console.WriteLine(regex.IsMatch(testing));
And it returned "true"
[no name] 8-Nov-12 3:22am    
I used this expression in regular expression validator and based on the result a validation call out extender is displayed. It's not working here
MT_ 8-Nov-12 3:23am    
can you share code snippet ?
MT_ 8-Nov-12 3:24am    
Or else try \d*[a-zA-Z]{1,}\d*
actually this /^[0-9]*[a-zA-Z_]+[a-zA-Z0-9_]*$/
actually this /^[a-zA-Z0-9_]*[a-zA-Z_]+[a-zA-Z0-9_]*$/
both don't allow numbers alone, both worked perfectly for me and their meaning is
[0-9]* - 0 or more times occurrences of 0-9 numbers any where in the entered string.
[a-zA-Z_]+ at least there must be one character in entered string from a-z, A-Z, _
[a-zA-Z0-9_]* There can be 0 or more repeated characters from a-z, A-Z, 0-9, _ any where in the entered string.
Here the allowed values to your input text-box are a-zA-Z0-9_ and not other than these characters eg:!@#%^&*()+= are not allowed.
 
Share this answer
 
Comments
CHill60 5-Jan-18 6:57am    
You've re-opened a 5-year old question that is already answered!
Stick to answering new questions where the OP still needs help.
lokesh jammugani 5-Jan-18 8:17am    
Okay..
Member 16002800 12-May-23 10:32am    
Thank you Lokesh, your example helps a lot!!

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