I need a help with regex which checks the string contains only letter and numbers but not only numbers
Valid
* letters
* 1wret
* 0123chars
* chars0123
* cha2rs
Invalid
* 1324
* xcvxxc%$#
* xcv123xxc%$#
* _012chars
* _test
To check whether a String contains only unicode letters or digits in Java, we use the isLetterOrDigit() method and charAt() method with decision-making statements. The isLetterOrDigit(char ch) method determines whether the specific character (Unicode ch) is either a letter or a digit.
You can use regular expressions to achieve this task. In order to verify that the string only contains letters, numbers, underscores and dashes, we can use the following regex: "^[A-Za-z0-9_-]*$".
To check if a string contains only numbers in JavaScript, call the test() method on this regular expression: ^\d+$ . The test() method will return true if the string contains only numbers. Otherwise, it will return false . The RegExp test() method searches for a match between a regular expression and a string.
The isalnum() method returns True if all characters in the string are alphanumeric (either alphabets or numbers). If not, it returns False.
Here are the components of the regex we're going to use:
^
and $
are the beginning and end of the string anchors respectively\d
matches a digit[a-zA-Z]
matches a letter[a-zA-Z\d]
matches a letter or a digit*
is "zero-or-more" repetitionWith these, we can now compose the regex we need (see on rubular.com):
^\d*[a-zA-Z][a-zA-Z\d]*$
Here's an explanation of the pattern:
from the beginning... till the end
| |
^\d*[a-zA-Z][a-zA-Z\d]*$
\_/\______/\_________/
The 3 parts are:
This should do it:
^[0-9]*[a-zA-Z]+[a-zA-Z0-9]*$
This requires at least one character of [a-zA-Z]
.
[a-z0-9]*[a-z]+[a-z0-9]*
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With