All Validators

Browse copy-paste ready snippets for your projects.

Is Adult (18+)

datetime

Checks if the provided date of birth makes the user at least 18 years old.

#dob#age#restriction
ts
const isAdult = (birthDate: Date): boolean => {
  const today = new Date();
  const age = today.getFullYear() - birthDate.getFullYear();
  const m = today.getMonth() - birthDate.getMonth();
  return age > 18 || (age === 18 && m >= 0 && today.getDate() >= birthDate.getDate());
};

Is Business Day

datetime

Checks if date is Mon-Fri.

#workday#weekday
ts
const isBusinessDay = (date: Date): boolean => {
  const day = date.getDay();
  return day !== 0 && day !== 6;
};

Is Weekend

datetime

Checks if date is Saturday or Sunday.

#weekend#holiday
ts
const isWeekend = (date: Date): boolean => date.getDay() === 0 || date.getDay() === 6;

Is Leap Year

datetime

Determines if a year is a leap year.

#calendar#year
ts
const isLeapYear = (year: number): boolean => (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;

Days Between Dates

datetime

Calculates number of days between two dates.

#diff#duration
ts
const daysBetween = (d1: Date, d2: Date): number => {
  const oneDay = 24 * 60 * 60 * 1000;
  return Math.round(Math.abs((d1.getTime() - d2.getTime()) / oneDay));
};

Is Future Date

datetime

Checks if a date is strictly in the future.

#validation#time
ts
const isFuture = (date: Date): boolean => date.getTime() > Date.now();

Is Valid Date Object

datetime

Checks if a Date object is valid and not "Invalid Date".

#check#error-handling
ts
const isValidDate = (d: any): boolean => d instanceof Date && !isNaN(d.getTime());

Is Past Date

datetime

Checks if a date is in the past.

#validation#time
ts
const isPast = (date: Date): boolean => date.getTime() < Date.now();

Is Same Day

datetime

Checks if two dates are on the same day.

#comparison#calendar
ts
const isSameDay = (d1: Date, d2: Date): boolean => d1.toDateString() === d2.toDateString();

Is Prime Number

math

Efficiently checks if a number is prime.

#algorithm#number
ts
const isPrime = (n: number): boolean => {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 === 0 || n % 3 === 0) return false;
  for (let i = 5; i * i <= n; i += 6) {
    if (n % i === 0 || n % (i + 2) === 0) return false;
  }
  return true;
};

Is Even

math

Checks if a number is even.

#basic#number
ts
const isEven = (n: number): boolean => n % 2 === 0;

Is Odd

math

Checks if a number is odd.

#basic#number
ts
const isOdd = (n: number): boolean => n % 2 !== 0;

Is Power of Two

math

Bitwise check if number is a power of 2.

#bitwise#binary#optimization
ts
const isPowerOfTwo = (n: number): boolean => n > 0 && (n & (n - 1)) === 0;

GCD (Greatest Common Divisor)

math

Euclidean algorithm for GCD.

#algorithm#math
ts
const gcd = (a: number, b: number): number => (!b ? a : gcd(b, a % b));

Clamp Number

math

Restricts a number within a min and max range.

#utility#bounds
ts
const clamp = (num: number, min: number, max: number): number => Math.min(Math.max(num, min), max);

Random Integer In Range

math

Generates a random integer between min and max (inclusive).

#random#rng
ts
const randomInt = (min: number, max: number): number => Math.floor(Math.random() * (max - min + 1)) + min;

Factorial

math

Calculates n! (recursive).

#algorithm#recursive
ts
const factorial = (n: number): number => (n <= 1 ? 1 : n * factorial(n - 1));

Fibonacci

math

Returns nth Fibonacci number.

#algorithm#sequence
ts
const fib = (n: number): number => n <= 1 ? n : fib(n - 1) + fib(n - 2);

Is Positive

math

Checks if number is positive.

#basic#number
ts
const isPositive = (n: number): boolean => n > 0;

Is Negative

math

Checks if number is negative.

#basic#number
ts
const isNegative = (n: number): boolean => n < 0;

Is Integer

math

Checks if number is an integer.

#type#number
ts
const isInteger = (n: number): boolean => Number.isInteger(n);

Is Float

math

Checks if number has decimal places.

#type#number
ts
const isFloat = (n: number): boolean => !Number.isInteger(n) && !isNaN(n);

Luhn Algorithm (Credit Card)

finance

Validates credit card numbers using checksum.

#payment#algorithm#validation
ts
const luhnCheck = (num: string): boolean => {
  let arr = (num + '').split('').reverse().map(x => parseInt(x));
  let lastDigit = arr.splice(0, 1)[0];
  let sum = arr.reduce((acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val * 2) % 9) || 9), 0);
  return (sum + lastDigit) % 10 === 0;
};

Format Currency

finance

Formats number as currency with locale.

#format#money
ts
const formatCurrency = (amount: number, locale = 'en-US', currency = 'USD'): string => 
  new Intl.NumberFormat(locale, { style: 'currency', currency }).format(amount);

What is CodeSnippets?

CodeSnippets is a comprehensive library of 200+ production-ready validation code snippets designed for developers who value efficiency and code quality. Our collection spans across multiple programming languages including JavaScript, TypeScript, Python, Go, PHP, C#, and Rust, ensuring you have the right validation logic regardless of your tech stack.

Every code snippet in our library is carefully crafted, tested, and optimized for real-world use cases. From simple email validation to complex credit card verification using the Luhn algorithm, from date calculations to XSS sanitization—we cover the validation patterns you encounter daily as a developer.

Our mission is simple: save you time and ensure code quality. Instead of writing validation logic from scratch or hunting through Stack Overflow, simply browse our categorized collection, find the validator you need, select your preferred programming language, and copy the production-ready code directly into your project.

Why Choose Our Code Snippets?

  • Production-Ready: All snippets are tested and ready for immediate use in your projects.
  • Multi-Language: Available in 7+ programming languages with consistent logic across all.
  • Categorized: Organized by use case—string validation, date/time, security, finance, and more.
  • MIT Licensed: Free to use in personal and commercial projects without restrictions.
  • Regularly Updated: Our library grows with new validators and language support.