Gundamaiah
Gundamaiah

Reputation: 786

How to Check that String is alphanumeric in Java

I am trying to check the entered String is alphanumeric or not.I tried some regular expressions i.e.,

 ^[a-zA-Z0-9]+$
 ^[A-Za-z0-9]*$
 ^[\pL\pN]+$

But all these will work if the String contains only numeric or contains only alphabets or alphanumeric. But I need to check the String should be AlphaNumeric and Starts with a digit only. Please help me out.

Upvotes: 1

Views: 2460

Answers (2)

TheLostMind
TheLostMind

Reputation: 36304

string.matches("[0-9]+[a-zA-Z]+[a-zA-Z0-9]*$"); 
// ensures you have atleast 1 number, 1 alphabet and starts with a number/digit

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336168

So you want at least one letter after the first digit. Right?

Then use

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

Upvotes: 0

Related Questions