Validation
Length Between 8 and 64 Regex
Many fields need only a length rule. This pattern accepts any single line of eight to sixty-four characters, which happens to be a common password length range, but it fits titles, short answers and reference codes too. The dot matches any character except a line break, so a value containing a newline fails, which is usually desirable for single-line inputs. Because JavaScript counts UTF-16 code units, some emoji count as two characters; add the u flag if you want each Unicode code point to count once. Change the two numbers to match the limits of your own field.
Pattern (JavaScript)
/^.{8,64}$/Starts at the start of the line/string, then any character, between 8 and 64 times, then the end of the line/string.
Examples
Matches
- ✓ 12345678
- ✓ Ali Test password
- ✓ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl
Does not match
- ✗ short
- ✗ abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm
- ✗ seven77
Try it
0 matches (JavaScript engine)
12345678
Ali Test password
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl
short
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm
seven77Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^.{8,64}$/g;
const text = "12345678";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^.{8,64}$')
text = '12345678'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^.{8,64}$/';
$text = '12345678';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^.{8,64}$");
String text = "12345678";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^.{8,64}$`)
text := "12345678"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^.{8,64}$");
var text = "12345678";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^.{8,64}$").unwrap();
let text = "12345678";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^.{8,64}$/
text = '12345678'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why does a value with a newline fail?
- The dot does not match line breaks without the s flag. That keeps single-line fields free of pasted newlines.
- How are emoji counted?
- Without the u flag, an emoji outside the Basic Multilingual Plane counts as two characters. Add the u flag to count each code point as one.
- Is a length check enough for passwords?
- Length is the most important single rule in current guidance. Pair it with a check against known breached passwords for better protection.