User
Can only contain letters in lowercase
function isValidUsername(username) {
return /^[a-z]+$/.test(username);
}
Password
Can contain any character, but must contain at least one lowercase letter, one uppercase letter, and one number.
Example 1
function isValidPassword(password) {
return /[a-z]/.test(password) &&
/[A-Z]/.test(password) &&
/[0-9]/.test(password);
}
Example 2 (recommended)
function isValidPassword(password) {
return /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/.test(password);
}
Telephone
Telephone number must be in the format of (555) 555-5555
function isValidTelephone(telephone) {
return /^\(\d{3}\)\s\d{3}\-\d{4}$/.test(telephone);
}
Must be a valid email address
function isValidEmail(email) {
return /^[^@]+@[^@.]+\.[a-z]+$/i.test(email);
}
My solution
function isValidEmail(email) {
return /^[a-z0-9-_]+@[a-z0-9-_]+\.[a-z]{2,}(\.[a-z]{2,})*$/i.test(email);
}
Color
function isHexColor(hexcolor) {
return /^#[0-9a-fA-F]{6}$/i.test(hexcolor);