Positive lookahead is what you're looking for. The regex looks like this:
(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]+
Here, (?=.*[A-Za-z])
is the positive lookahead which asserts that your string as at least one character, and (?=.*[0-9])
asserts that it has at least one digit. It's important to note that the positive lookahead doesn't return a match, but rather asserts whether a match exists or not. So, you should read the previous regex as "assert that it has at least one character; assert it has at least one digit; now that we know the assertions have passed, just check for alphanumeric characters".
This is very interesting because it allows you to easily combine the validation requirements of your application, without making your regex very complex. For example, if you now require the string to have exactly 20 characters, you just need to add a new positive lookahead assertion, like so:
(?=[A-Za-z0-9]{20})(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]+
Hope it helps!