Skip to main content

Try it Live

Run Base64 examples in the interactive playground

Validation

Methods for validating base64 string formats before decoding.

Standard Base64 Validation

Base64.isValid(str)

Checks if string is valid standard base64 format. Validates alphabet, length, and padding requirements.
Parameters:
  • str: string - String to validate
Returns: boolean - true if valid standard base64, false otherwise Validation rules:
  • Alphabet: Only A-Z, a-z, 0-9, +, /
  • Length: Must be multiple of 4 characters
  • Padding: 0-2 = characters at end only
  • Empty: Empty string is valid
Example:
Defined in: primitives/Base64/BrandedBase64/isValid.js:7 Implementation:

URL-Safe Base64 Validation

Base64.isValidUrlSafe(str)

Checks if string is valid URL-safe base64 format. Validates URL-safe alphabet without padding requirements.
Parameters:
  • str: string - String to validate
Returns: boolean - true if valid URL-safe base64, false otherwise Validation rules:
  • Alphabet: Only A-Z, a-z, 0-9, -, _
  • Length: No length requirement (padding removed)
  • Padding: No padding characters allowed
  • Empty: Empty string is valid
Example:
Defined in: primitives/Base64/BrandedBase64/isValidUrlSafe.js:7 Implementation:

Usage Patterns

Type-Safe Validation

Format Detection

Validation with Error Messages

Pre-decode Validation

Batch Validation

Validation with Conversion

Validation Examples

Valid Standard Base64

Valid URL-Safe Base64

Invalid Examples

Validation Algorithm

Standard base64 validation:
  1. Check empty string (valid)
  2. Check regex: /^[A-Za-z0-9+/]*={0,2}$/
  3. Check length: length % 4 === 0
  4. Verify decode: atob(str) succeeds
URL-safe base64 validation:
  1. Check empty string (valid)
  2. Check regex: /^[A-Za-z0-9_-]*$/

Performance

Validation is fast and should be done before decoding:
  • isValid(): ~1μs per call (includes regex + atob)
  • isValidUrlSafe(): ~0.5μs per call (regex only)