Skip to content

Instantly share code, notes, and snippets.

@jaredkc
Created June 6, 2023 13:37
Show Gist options
  • Save jaredkc/32c9910e1b97426036323f162bfbb896 to your computer and use it in GitHub Desktop.
Save jaredkc/32c9910e1b97426036323f162bfbb896 to your computer and use it in GitHub Desktop.
Form Utilities
export function isValidEmail(email: string): boolean {
const re = /\S+@\S+\.\S+/;
const emailTest = re.test(email);
return emailTest;
}
export function isSecurePassword(password: string): boolean {
// TODO: remove this once we are past testing data
if (password.includes('testing')) return true;
// Password must be at least 8 characters long, contain at least one uppercase letter,
// one lowercase letter, one number, and can have special characters.
// (?=.*[a-z]) - at least one lowercase letter
// (?=.*[A-Z]) - at least one uppercase letter
// (?=.*\d) - at least one number
// [a-zA-Z0-9_!@#$%^&*()+?><.=\d]{8,} - at least 8 characters, special characters are optional
const re = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z0-9_!@#$%^&*()+?><.=\d]{8,}$/;
const passwordTest = re.test(password);
return passwordTest;
}
function randomCharacters(characters: string, count: number): string[] {
const items = [];
const array = characters.toString().split('');
for (let i = 0; i < count; i++) {
items.push(array[Math.floor(Math.random() * array.length)]);
}
return items;
}
export function generatePassword(): string {
const letters = 'abcdefghijklmnopqrstuvwxyz';
const numbers = '0123456789';
const specialCharacters = '_!@#$%^&*()+?><.=';
const password = [
...randomCharacters(letters, 4),
...randomCharacters(letters.toUpperCase(), 4),
...randomCharacters(numbers, 2),
...randomCharacters(specialCharacters, 2),
];
const shuffled = password.sort(() => 0.5 - Math.random());
return shuffled.toString().replaceAll(',', '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment