export const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ export const combineValidators = (...validators: any) => (...value: any) => { for (let i = 0; i < validators.length; ++i) { const error_message = validators[i](...value) if (error_message) return error_message } } export function isFalsy(item: any) { try { if ( !item || // handles most, like false, 0, null, etc (typeof item === 'object' && Object.keys(item).length === 0 && // for empty objects, like {}, [] !(typeof item.addEventListener === 'function')) // omit webpage elements ) { return true } } catch (err) { return true } return false } export const requiredValidator = ( value?: string | number | Array | boolean, message?: string ): string => { let errorMessage = '' if (isFalsy(typeof value === 'string' ? value.trim() : value)) { errorMessage = message || 'Required' } return errorMessage } export const emailValidator = (email: string) => !emailRegex.test(email) ? 'Please enter a valid email address' : '' export const passwordValidator = (password: string): string => { if (!password || password.length < 6) { return 'The password must be at least 6 characters.' } return '' } export const confirmPasswordValidator = (confirmPassword: string, password: string): string => { if (confirmPassword !== password) { return 'Passwords do not match.' } return '' } export const confirmEmailValidator = (confirmEmail: string, email: string): string => { if (confirmEmail !== email) { return 'Emails do not match.' } return '' }