I want to validate the input data to allow only alphanumeric value. It should not be only numbers or only alphabets. How to do that using regex expression in Java?
Asked
Active
Viewed 411 times
-9
-
You can find another resolve ways here [How to create a regex for accepting only alphanumeric characters?](http://stackoverflow.com/questions/5988228/how-to-create-a-regex-for-accepting-only-alphanumeric-characters) – Duy Huynh Dec 21 '15 at 10:23
1 Answers
2
You can very easily find the character class for alphanumeric characters in the Pattern javadoc:
\p{Alnum}
An alphanumeric character:[\p{Alpha}\p{Digit}]

Andy Turner
- 137,514
- 11
- 162
- 243
-
1why you provide an answer when OP does not have any research efforts? – codegasmer Dec 21 '15 at 10:09
-
@codegasmer sometimes a little nudge in the right direction helps. – Andy Turner Dec 21 '15 at 10:11
-
thanks Andy .. i have tried this way a.matches("^[A-Za-z0-9]+$") but this allows data with numbers also .. can u suggest few other ways as well – user5691531 Dec 21 '15 at 10:16
-
Oh, sorry - *only* numbers **OR** *only* alphabets? That's not really what alphanumeric data is. You should really try to work out how to do it yourself from the Javadoc (look for "Logical operators"). – Andy Turner Dec 21 '15 at 10:19
-
should allow only alphanumeric input not just the alphabets or numbers – user5691531 Dec 21 '15 at 10:52