How do I create a regular expression of minimum 8, maximum 16, alphabetic, numbers and NO space?
Categories:
Crafting a Robust Regex: Alphanumeric, No Spaces, Min/Max Length

Learn how to construct a regular expression that validates input strings to be between 8 and 16 characters long, containing only alphabetic letters and numbers, with no spaces.
Regular expressions (regex) are powerful tools for pattern matching in strings. A common requirement in forms and data validation is to ensure that user input adheres to specific criteria, such as length, allowed characters, and disallowed characters. This article will guide you through building a regex that enforces a minimum length of 8, a maximum length of 16, allows only alphabetic characters and numbers, and explicitly forbids spaces.
Understanding the Requirements
Before diving into the regex itself, let's break down each requirement to understand how it translates into regex components:
- Minimum 8 characters: This implies that the string must have at least 8 characters.
- Maximum 16 characters: The string cannot exceed 16 characters in total.
- Alphabetic characters: Only letters (a-z, A-Z) are permitted.
- Numbers: Only digits (0-9) are permitted.
- NO space: Spaces are explicitly forbidden anywhere in the string.
flowchart TD A[Start] --> B{"Input String? (e.g., 'password123')"} B --> C{Length Check (Min 8, Max 16)?} C -- No --> F[Invalid] C -- Yes --> D{Character Check (Alphabetic/Numeric Only)?} D -- No --> F[Invalid] D -- Yes --> E{Space Check (No Spaces)?} E -- No --> F[Invalid] E -- Yes --> G[Valid] F --> H[End] G --> H[End]
Flowchart illustrating the validation logic for the regex requirements.
Building the Regular Expression
We'll construct the regex piece by piece, combining character classes, quantifiers, and anchors to meet all the specified criteria.
Regex Components Explained
Let's look at the individual parts that make up our final regex:
^
The caret ^
asserts the position at the start of the string.
[a-zA-Z0-9]
This character class matches any single uppercase letter, lowercase letter, or digit. This addresses the 'alphabetic and numbers' requirement.
{8,16}
This quantifier specifies that the preceding element (our character class) must occur at least 8 times and at most 16 times. This handles the minimum and maximum length requirements.
$
The dollar sign $
asserts the position at the end of the string. Together with ^
, it ensures that the entire string must match the pattern, preventing extra characters (like spaces) from being present outside the defined range.
The Complete Regular Expression
Combining these components, we get the following regular expression:
^[a-zA-Z0-9]{8,16}$
The complete regular expression for validating alphanumeric strings with specific length constraints.
Let's break down how this works:
^
: Matches the beginning of the string.[a-zA-Z0-9]
: Matches any single alphanumeric character (a-z, A-Z, 0-9).{8,16}
: Ensures that the preceding character class appears between 8 and 16 times, inclusive.$
: Matches the end of the string.
The combination of ^
and $
is crucial. Without them, the regex would match any substring that meets the criteria, rather than the entire string. For example, abc defgh123
would match defgh123
if ^
and $
were omitted, but with them, it correctly fails because of the space.
Practical Examples
Here are some examples of how this regex would behave with different inputs:
Valid Examples
password123 MyPass12 TESTING1234567890 shortpwd
Invalid Examples
too_short thisiswaytoolong1234567890 contains space !@#$invalid
\w
(word characters: [a-zA-Z0-9_]
). However, \w
includes the underscore _
, which is not allowed by the problem statement. Therefore, [a-zA-Z0-9]
is the more precise choice here.Implementation in Various Languages
The regex itself is largely universal, but its implementation varies slightly depending on the programming language.
JavaScript
const regex = /^[a-zA-Z0-9]{8,16}$/;
console.log(regex.test("password123")); // true console.log(regex.test("contains space")); // false console.log(regex.test("short")); // false
Python
import re
regex = r"^[a-zA-Z0-9]{8,16}$"
print(re.match(regex, "password123")) # <re.Match object; span=(0, 11), match='password123'> print(re.match(regex, "contains space")) # None print(re.match(regex, "short")) # None
PHP
Java
import java.util.regex.Matcher; import java.util.regex.Pattern;
public class RegexTest { public static void main(String[] args) { String regex = "^[a-zA-Z0-9]{8,16}$"; System.out.println(Pattern.matches(regex, "password123")); // true System.out.println(Pattern.matches(regex, "contains space")); // false System.out.println(Pattern.matches(regex, "short")); // false } }
This comprehensive regular expression provides a robust solution for validating strings that must meet specific length, character set, and exclusion criteria. By understanding each component, you can adapt this pattern for similar validation needs in your projects.