Regex for TOTP Secret (Base32)
Regex Pattern
^[A-Z2-7]{16,}$Base32-encoded TOTP secret key (16+ uppercase letters and 2-7)
Quick Answer
The regex pattern for totp secret (base32) is `^[A-Z2-7]{16,}$`. Base32-encoded TOTP secret key (16+ uppercase letters and 2-7). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| JBSWY3DPEHPK3PXP | ✓ Matches |
| MFZWIZLTOQ2TKZJX | ✓ Matches |
| short | ✗ No match |
| lowercase1234567 | ✗ No match |
| INVALID89! | ✗ No match |
Code Examples
javascript
const regex = /^[A-Z2-7]{16,}$/;
const isValid = regex.test(value);python
import re
pattern = r'^[A-Z2-7]{16,}$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^[A-Z2-7]{16,}$/
if value =~ pattern
puts "valid"
endphp
if (preg_match('/^[A-Z2-7]{16,}$/', $value)) {
echo "valid";
}java
String pattern = "^[A-Z2-7]{16,}$";
boolean isValid = value.matches(pattern);Frequently Asked Questions
Related Regex Patterns
UUID v4
UUID version 4 (randomly generated)
UUID (Any Version)
Any RFC 4122 UUID (versions 1-5)
JWT Token
JSON Web Token (header.payload.signature)
Hex String
Any-length hexadecimal string
Base64 String
Standard base64 encoded string
Alphanumeric Username
Username with letters, digits, underscores (3-16 chars)