UNPKG

react-tailwind-form-validator

Version:

A lightweight and customizable React form validation library built with Tailwind CSS for modern, responsive, and accessible forms.

45 lines (44 loc) 1.55 kB
// ALL REGEX FROM THE INTERNET, PLEASE CONSIDER INTERNET FOR CLARIFICATION // Don't blame me ^_^ export const validateEmail = (value) => { if (!value) return 'Email is required.'; const emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; return emailPattern.test(value) ? '' : 'Please enter a valid email address.'; }; export const validatePassword = (value, security = 'low') => { if (!value) return 'Password is required.'; if (value.length < 8) return 'At least 8 characters required.'; if (!/[A-Z]/.test(value)) return 'Add at least one uppercase letter.'; if (security === 'max' && !/[!@#$%^&*(),.?":{}|<>]/.test(value)) return 'Include one symbol.'; if (security !== 'max' && /[!@#$%^&*(),.?":{}|<>]/.test(value)) return 'Symbols are not allowed.'; return ''; }; export const validateDate = (value) => { if (!value) return 'Date is required.'; return isNaN(Date.parse(value)) ? 'Please enter a valid date.' : ''; }; export const validateNumber = (value) => { if (!value) return 'Number is required.'; return isNaN(Number(value)) ? 'Please enter a valid number.' : ''; }; export const capitalizeFirstLetter = (val) => { if (!val) return ''; return val.charAt(0).toUpperCase() + val.slice(1); }; export const debounce = (fn, timeout) => { let timer; return (...args) => { if (timer) clearTimeout(timer); timer = setTimeout(() => fn(...args), timeout); }; };