Click here to Skip to main content
15,066,168 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
   
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*
[no name] 8-Nov-12 3:30am
   
q1r1s1 1q2rs3
MT_ 8-Nov-12 3:32am
   
What ?
[no name] 8-Nov-12 3:32am
   
these are treated as invalid

I am looking for an expression that should check one alphabet at least irrespective of position in a string
[no name] 8-Nov-12 3:35am
   
the string should contain atleast one alphabet irrespective of position

either front or rare or middle ..... atleast one alphabet means, not just numbers alone.
[no name] 8-Nov-12 3:38am
   
I already told that I dont know regular expressions, sorry for irking u
MT_ 8-Nov-12 3:43am
   
Try now. Updated the regex in the solution.
[no name] 8-Nov-12 3:49am
   
solution....?
MT_ 8-Nov-12 3:56am
   
Check the code I supplied above once again, I have updated regex and some sample.
[no name] 8-Nov-12 4:02am
   
<asp:RegularExpressionValidator ID="regExValidator" runat="server" ControlToValidate="txtKeyName" Display="None"
ErrorMessage="Numerics Alone are not Allowd" SetFocusOnError="true" ValidationGroup="test" ValidationExpression=".*[a-zA-Z]+.*">
[no name] 8-Nov-12 4:03am
   
".*[a-zA-Z]+.*" working fine.... thank you for your time...
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.
   
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..

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