DD
DevDash

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

InputResult
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"
end

php

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

Want API access + no ads? Pro coming soon.