isBase64

Checks if a string is a valid base64 encoded string. This is a utility Typescript function that can be copy & pasted into your codebase.

Step-By-Step Guide

1. Copy the Source Code

@/lib/utils.tsx

1/**
2 * Checks if a string is a valid base64 encoded string.
3 *
4 * @param str - The string to be checked.
5 * @returns A boolean indicating whether the string is base64 encoded or not.
6 */
7export function isBase64(str: string): boolean {
8	const base64Regex =
9		/^(?:[A-Za-z0-9+\/]{4})*?(?:[A-Za-z0-9+\/]{2}(?:==)?|[A-Za-z0-9+\/]{3}=?)?$/;
10	return base64Regex.test(str);
11}

2. Use it in a file

/page.tsx

1import { isBase64 } from '@/lib/utils'; 
2
3const encodedString = 'R290IFlhIQ==';
4console.log(isBase64(encodedString)); // Outputs: true

3. Examples

/page.tsx

1// URL PARAM CHECK
2import { isBase64 } from '@/lib/utils';
3
4export default function Page() {
5    const searchParams = useSearchParams();
6    const hopefullyBase64EncodedParam = searchParams.get('q');
7
8    if(!isBase64(hopefullyBase64EncodedParam)){
9        //Do something
10    }
11    return({/*YOUR CODE*/})
12}

Tech Stack

Only the finest ingredients are used in our products. We made sure that we only use the best technologies available which offer a free tier! Yes, you read that right. Everything can be set up for FREE!

TypeScript

TypeScript

TypeScript ensures that your code is always correct. It is the best way to write JavaScript.

Programming Language

You May Also Like

Didn't find what you were looking for? Check out these other items.

Ui Component Library Template
Internal link to /templates/ui-component-library
Templates

Ui Component Library

Want to create your own Ui Component Library like shadcn/ui? This is your template!

Product Image
Internal link to /functions/copy-to-clipboard
Functions

Copy To Clipboard

Copies the given string to the clipboard.

Background Pattern | BoilerplateHQ
Internal link to /functions/background-pattern
Functions

Background Pattern

Provides a background image URL based on a predefined pattern.

Frequently Asked Questions

We have compiled a list of frequently asked questions. If you have any other questions, please do not hesitate to contact us via email or using the chat function. We are here to help!

Interested in kickstarting your next project?

Get the latest boilerplates, components, and news straight to your inbox.

By signing up, you agree to our Privacy Policy