Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a textbox in which i want validation like as per following

if a user enters only alphabets it should allow
if a user enters alphanumeric it should allow
but if a user enters only numbers it should not allow



i knew some of them but i doesnt work
^[0-9]{1,17} : Allow only number
^[a-zA-z]{1,50} : Allow Only Text
^[a-zA-Z0-9-\s&]{1,255} : Allow only Alphanumeric text


please help me through this
Posted

Who told you that Regex should be used everywhere or it's the best in this case? Use the tools only where they are most adequate.

In this very case, some non-Regex approach should be way easy and more maintainable. Pay attention: all your rules are not based on any certain patterns, they are only related to multiplicity. Therefore, calculate number of characters classified into 3 classes: digits, letters and the rest of them. For that purpose, traverse the string in a foreach loop and increment one of the two counters (for digits and letters, the third count will be obtained by subtraction from the string Length), using two functions:
http://msdn.microsoft.com/en-us/library/7f0ddtxh.aspx[^],
http://msdn.microsoft.com/en-us/library/yyxz6h5w.aspx[^].

That's all. Simple, isn't it?

—SA
 
Share this answer
 
Well, what about this?

^[a-zA-Z]*[0-9]*[a-zA-Z]+[0-9]*

I have tested it on Editplus. Tested samples are like this.
VB
asdfasdffd
1231232131
1232131adfasdfas
asdfs1231asdfas
asfsfs13123

Searched results are like this.
VB
asdfasdffd
1232131adfasdfas
asdfs1231asdfas
asfsfs13123

I hope it would be helpful to you.
 
Share this answer
 
Comments
webv12 25-Oct-13 0:15am    
no its not working...
I want like if a user enters only number it should deny
if a user enters alphabets or alphanumeric it should accept
the required expression for this is
^[a-zA-Z\s_][a-zA-Z0-9\s_]+$

the above expression works as following
it will allow alphabets
it will allow alphanumeric
it wont allow only numbers
 
Share this answer
 
Comments
WuRunZhe 25-Oct-13 2:30am    
Is it right?
According to your expression, user must input password like this.

First character must be alphabet and the others can be alphanumerics.

Your expression can pass this sample?

123abcdef123
webv12 25-Oct-13 2:44am    
no my expression doesnt allow 123abcdef123
my expression does allow alphanumeric only if it starts with some alphabet first
WuRunZhe 25-Oct-13 2:52am    
Ok.

But according to your questions, you didn't say that your expression does start with some alphabet first.

Am I right?

And in my solution, I said that I tested on EditPlus. Didn't you see that? Why my answers has been downvoted?

Have you got right?
webv12 25-Oct-13 5:18am    
ok ok my mistake chill dude

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