Spaces:
Sleeping
Sleeping
| /** | |
| * Form validation utilities | |
| */ | |
| export function validateEmail(email) { | |
| const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | |
| return regex.test(email); | |
| } | |
| export function validatePhone(phone) { | |
| const regex = /^[6-9]\d{9}$/; | |
| return regex.test(phone.replace(/\s|-/g, '')); | |
| } | |
| export function validatePAN(pan) { | |
| const regex = /^[A-Z]{5}[0-9]{4}[A-Z]{1}$/; | |
| return regex.test(pan.toUpperCase()); | |
| } | |
| export function validatePassword(password) { | |
| // Minimum 8 characters, at least 1 uppercase, 1 lowercase, 1 number | |
| const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/; | |
| return regex.test(password); | |
| } | |
| export function validateRequired(value) { | |
| return value !== undefined && value !== null && value.toString().trim() !== ''; | |
| } | |
| export function validateMinLength(value, min) { | |
| return value && value.length >= min; | |
| } | |
| export function validateMaxLength(value, max) { | |
| return !value || value.length <= max; | |
| } | |
| export function validateAmount(amount) { | |
| const num = parseFloat(amount); | |
| return !isNaN(num) && num > 0 && num <= 10000000; | |
| } | |
| export function validatePincode(pincode) { | |
| return /^\d{6}$/.test(pincode); | |
| } | |
| /** | |
| * Validate full form | |
| * @param {Object} fields - { fieldName: { value, validators: [{ fn, message }] } } | |
| * @returns {{ isValid, errors }} | |
| */ | |
| export function validateForm(fields) { | |
| const errors = {}; | |
| let isValid = true; | |
| Object.entries(fields).forEach(([field, config]) => { | |
| const { value, validators = [] } = config; | |
| for (const { fn, message } of validators) { | |
| if (!fn(value)) { | |
| errors[field] = message; | |
| isValid = false; | |
| break; | |
| } | |
| } | |
| }); | |
| return { isValid, errors }; | |
| } | |