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.
str: string- String to validate
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
URL-Safe Base64 Validation
Base64.isValidUrlSafe(str)
Checks if string is valid URL-safe base64 format. Validates URL-safe alphabet without padding requirements.
str: string- String to validate
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
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:- Check empty string (valid)
- Check regex:
/^[A-Za-z0-9+/]*={0,2}$/ - Check length:
length % 4 === 0 - Verify decode:
atob(str)succeeds
- Check empty string (valid)
- 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)
Related
- Encoding - Encoding to base64 format
- Decoding - Decoding base64 strings
- Utilities - Size calculations
- BrandedBase64 - Tree-shakeable API

