* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 40px 20px;
}
.container {
max-width: 700px;
margin: 0 auto;
}
.header {
text-align: center;
color: white;
margin-bottom: 40px;
}
.header h1 {
font-size: 2.5rem;
font-weight: 700;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}
.header p {
font-size: 1.1rem;
opacity: 0.95;
}
.form-card {
background: white;
border-radius: 20px;
padding: 40px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.form-title {
font-size: 1.8rem;
color: #2c3e50;
margin-bottom: 30px;
text-align: center;
}
.form-group {
margin-bottom: 25px;
position: relative;
}
.form-group label {
display: block;
color: #555;
font-weight: 600;
margin-bottom: 8px;
font-size: 14px;
}
.form-group label .required {
color: #e74c3c;
margin-left: 4px;
}
.input-wrapper {
position: relative;
}
.form-control {
width: 100%;
padding: 14px 45px 14px 15px;
border: 2px solid #e0e0e0;
border-radius: 10px;
font-size: 15px;
font-family: inherit;
transition: all 0.3s;
background: white;
}
.form-control:focus {
outline: none;
border-color: #27ae60;
box-shadow: 0 0 0 3px rgba(39, 174, 96, 0.1);
}
.form-control.error {
border-color: #e74c3c;
background: #fff5f5;
}
.form-control.success {
border-color: #27ae60;
background: #f0fff4;
}
.validation-icon {
position: absolute;
right: 15px;
top: 50%;
transform: translateY(-50%);
font-size: 20px;
display: none;
}
.validation-icon.show {
display: block;
}
.validation-icon.success {
color: #27ae60;
}
.validation-icon.error {
color: #e74c3c;
}
.error-message {
color: #e74c3c;
font-size: 13px;
margin-top: 6px;
display: none;
animation: shake 0.3s;
}
.error-message.show {
display: block;
}
@keyframes shake {
0%, 100% { transform: translateX(0); }
25% { transform: translateX(-5px); }
75% { transform: translateX(5px); }
}
.success-message {
color: #27ae60;
font-size: 13px;
margin-top: 6px;
display: none;
}
.success-message.show {
display: block;
}
.password-strength {
margin-top: 10px;
display: none;
}
.password-strength.show {
display: block;
}
.strength-bar {
height: 6px;
background: #e0e0e0;
border-radius: 10px;
overflow: hidden;
margin-bottom: 8px;
}
.strength-fill {
height: 100%;
width: 0;
transition: all 0.3s;
border-radius: 10px;
}
.strength-fill.weak {
width: 33%;
background: #e74c3c;
}
.strength-fill.medium {
width: 66%;
background: #f39c12;
}
.strength-fill.strong {
width: 100%;
background: #27ae60;
}
.strength-text {
font-size: 12px;
font-weight: 600;
}
.strength-text.weak {
color: #e74c3c;
}
.strength-text.medium {
color: #f39c12;
}
.strength-text.strong {
color: #27ae60;
}
.password-requirements {
margin-top: 10px;
padding: 12px;
background: #f8f9fa;
border-radius: 8px;
font-size: 12px;
}
.requirement {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 5px;
color: #777;
}
.requirement:last-child {
margin-bottom: 0;
}
.requirement.met {
color: #27ae60;
}
.requirement-icon {
font-size: 14px;
}
.checkbox-group {
display: flex;
align-items: center;
gap: 10px;
margin-top: 20px;
}
.checkbox-group input[type="checkbox"] {
width: 18px;
height: 18px;
cursor: pointer;
accent-color: #27ae60;
}
.checkbox-group label {
margin: 0;
cursor: pointer;
font-weight: 400;
font-size: 14px;
}
.btn-submit {
width: 100%;
padding: 16px;
background: linear-gradient(135deg, #27ae60 0%, #229954 100%);
color: white;
border: none;
border-radius: 12px;
font-size: 16px;
font-weight: 700;
cursor: pointer;
transition: all 0.3s;
margin-top: 30px;
box-shadow: 0 4px 15px rgba(39, 174, 96, 0.3);
text-transform: uppercase;
letter-spacing: 1px;
}
.btn-submit:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(39, 174, 96, 0.4);
}
.btn-submit:active {
transform: translateY(0);
}
.btn-submit:disabled {
background: linear-gradient(135deg, #95a5a6 0%, #7f8c8d 100%);
cursor: not-allowed;
transform: none;
}
.character-count {
font-size: 12px;
color: #777;
margin-top: 5px;
text-align: right;
}
.character-count.warning {
color: #f39c12;
}
.character-count.error {
color: #e74c3c;
}
.toast {
position: fixed;
top: 30px;
right: 30px;
background: white;
padding: 20px 25px;
border-radius: 12px;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
display: none;
align-items: center;
gap: 15px;
z-index: 1000;
animation: slideInRight 0.3s ease;
min-width: 300px;
}
@keyframes slideInRight {
from {
transform: translateX(400px);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
.toast.show {
display: flex;
}
.toast.success {
border-left: 4px solid #27ae60;
}
.toast.error {
border-left: 4px solid #e74c3c;
}
.toast-icon {
width: 40px;
height: 40px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 20px;
}
.toast.success .toast-icon {
background: #d4edda;
color: #27ae60;
}
.toast.error .toast-icon {
background: #f8d7da;
color: #e74c3c;
}
.toast-content h4 {
font-size: 15px;
margin-bottom: 3px;
color: #2c3e50;
}
.toast-content p {
font-size: 13px;
color: #777;
margin: 0;
}
select.form-control {
cursor: pointer;
}
@media (max-width: 768px) {
.form-card {
padding: 30px 20px;
}
.header h1 {
font-size: 2rem;
}
.toast {
top: 15px;
right: 15px;
left: 15px;
}
}
const form = document.getElementById('registrationForm');
const fields = {
fullName: document.getElementById('fullName'),
email: document.getElementById('email'),
phone: document.getElementById('phone'),
username: document.getElementById('username'),
password: document.getElementById('password'),
confirmPassword: document.getElementById('confirmPassword'),
age: document.getElementById('age'),
country: document.getElementById('country'),
bio: document.getElementById('bio'),
terms: document.getElementById('terms')
};
// Validation rules
const validations = {
fullName: {
required: true,
minLength: 2,
pattern: /^[a-zA-Z\s]+$/,
messages: {
required: 'Full name is required',
minLength: 'Name must be at least 2 characters',
pattern: 'Name can only contain letters and spaces'
}
},
email: {
required: true,
pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
messages: {
required: 'Email is required',
pattern: 'Please enter a valid email address'
}
},
phone: {
required: true,
pattern: /^[\d\s\-\+\(\)]+$/,
minLength: 10,
messages: {
required: 'Phone number is required',
pattern: 'Please enter a valid phone number',
minLength: 'Phone number must be at least 10 digits'
}
},
username: {
required: true,
minLength: 3,
maxLength: 20,
pattern: /^[a-zA-Z0-9_]+$/,
messages: {
required: 'Username is required',
minLength: 'Username must be at least 3 characters',
maxLength: 'Username cannot exceed 20 characters',
pattern: 'Username can only contain letters, numbers, and underscores'
}
},
password: {
required: true,
minLength: 8,
custom: validatePassword,
messages: {
required: 'Password is required',
minLength: 'Password must be at least 8 characters',
custom: 'Password does not meet requirements'
}
},
confirmPassword: {
required: true,
match: 'password',
messages: {
required: 'Please confirm your password',
match: 'Passwords do not match'
}
},
age: {
required: true,
min: 18,
max: 120,
messages: {
required: 'Age is required',
min: 'You must be at least 18 years old',
max: 'Please enter a valid age'
}
},
country: {
required: true,
messages: {
required: 'Please select a country'
}
}
};
// Real-time validation
Object.keys(fields).forEach(fieldName => {
if (fields[fieldName] && fieldName !== 'terms') {
fields[fieldName].addEventListener('input', () => validateField(fieldName));
fields[fieldName].addEventListener('blur', () => validateField(fieldName));
}
});
// Character counter for bio
fields.bio.addEventListener('input', () => {
const count = fields.bio.value.length;
const counter = document.getElementById('charCount');
const counterEl = counter.parentElement;
counter.textContent = count;
if (count > 180) {
counterEl.classList.add('warning');
} else {
counterEl.classList.remove('warning');
}
if (count >= 200) {
counterEl.classList.add('error');
} else {
counterEl.classList.remove('error');
}
});
// Password strength
fields.password.addEventListener('input', () => {
checkPasswordStrength();
if (fields.confirmPassword.value) {
validateField('confirmPassword');
}
});
function validateField(fieldName) {
const field = fields[fieldName];
const rules = validations[fieldName];
if (!rules) return true;
const value = field.value.trim();
const wrapper = field.closest('.form-group');
const errorMsg = wrapper.querySelector('.error-message');
const successMsg = wrapper.querySelector('.success-message');
const icon = wrapper.querySelector('.validation-icon');
// Check required
if (rules.required && !value) {
showError(field, errorMsg, icon, rules.messages.required);
return false;
}
// Check minLength
if (rules.minLength && value.length < rules.minLength) {
showError(field, errorMsg, icon, rules.messages.minLength);
return false;
}
// Check maxLength
if (rules.maxLength && value.length > rules.maxLength) {
showError(field, errorMsg, icon, rules.messages.maxLength);
return false;
}
// Check pattern
if (rules.pattern && !rules.pattern.test(value)) {
showError(field, errorMsg, icon, rules.messages.pattern);
return false;
}
// Check min/max for numbers
if (rules.min && parseFloat(value) < rules.min) {
showError(field, errorMsg, icon, rules.messages.min);
return false;
}
if (rules.max && parseFloat(value) > rules.max) {
showError(field, errorMsg, icon, rules.messages.max);
return false;
}
// Check match
if (rules.match && value !== fields[rules.match].value) {
showError(field, errorMsg, icon, rules.messages.match);
return false;
}
// Custom validation
if (rules.custom && !rules.custom(value)) {
showError(field, errorMsg, icon, rules.messages.custom);
return false;
}
showSuccess(field, errorMsg, successMsg, icon);
return true;
}
function showError(field, errorMsg, icon, message) {
field.classList.remove('success');
field.classList.add('error');
errorMsg.textContent = message;
errorMsg.classList.add('show');
icon.classList.remove('show', 'success');
icon.classList.add('show', 'error');
icon.textContent = '✗';
}
function showSuccess(field, errorMsg, successMsg, icon) {
field.classList.remove('error');
field.classList.add('success');
errorMsg.classList.remove('show');
icon.classList.remove('error');
icon.classList.add('show', 'success');
icon.textContent = '✓';
}
function validatePassword(password) {
const requirements = {
length: password.length >= 8,
uppercase: /[A-Z]/.test(password),
lowercase: /[a-z]/.test(password),
number: /\d/.test(password),
special: /[!@#$%^&*(),.?":{}|<>]/.test(password)
};
return Object.values(requirements).every(req => req);
}
function checkPasswordStrength() {
const password = fields.password.value;
const strengthBar = document.querySelector('.strength-fill');
const strengthText = document.querySelector('.strength-text');
const strengthContainer = document.querySelector('.password-strength');
const requirements = document.querySelectorAll('.requirement');
if (!password) {
strengthContainer.classList.remove('show');
return;
}
strengthContainer.classList.add('show');
const checks = {
length: password.length >= 8,
uppercase: /[A-Z]/.test(password),
lowercase: /[a-z]/.test(password),
number: /\d/.test(password),
special: /[!@#$%^&*(),.?":{}|<>]/.test(password)
};
// Update requirement indicators
requirements.forEach(req => {
const type = req.dataset.requirement;
const icon = req.querySelector('.requirement-icon');
if (checks[type]) {
req.classList.add('met');
icon.textContent = '✓';
} else {
req.classList.remove('met');
icon.textContent = '○';
}
});
// Calculate strength
const metCount = Object.values(checks).filter(Boolean).length;
strengthBar.className = 'strength-fill';
strengthText.className = 'strength-text';
if (metCount <= 2) {
strengthBar.classList.add('weak');
strengthText.classList.add('weak');
strengthText.textContent = 'Weak';
} else if (metCount <= 4) {
strengthBar.classList.add('medium');
strengthText.classList.add('medium');
strengthText.textContent = 'Medium';
} else {
strengthBar.classList.add('strong');
strengthText.classList.add('strong');
strengthText.textContent = 'Strong';
}
}
// Form submission
form.addEventListener('submit', (e) => {
e.preventDefault();
let isValid = true;
// Validate all fields
Object.keys(validations).forEach(fieldName => {
if (!validateField(fieldName)) {
isValid = false;
}
});
// Validate terms
const termsError = document.getElementById('termsError');
if (!fields.terms.checked) {
termsError.textContent = 'You must agree to the terms and conditions';
termsError.classList.add('show');
isValid = false;
} else {
termsError.classList.remove('show');
}
if (isValid) {
showToast('Success!', 'Account created successfully', 'success');
setTimeout(() => {
form.reset();
document.querySelectorAll('.form-control').forEach(field => {
field.classList.remove('success', 'error');
});
document.querySelectorAll('.validation-icon').forEach(icon => {
icon.classList.remove('show');
});
document.querySelector('.password-strength').classList.remove('show');
}, 2000);
} else {
showToast('Error!', 'Please fix the errors in the form', 'error');
}
});
function showToast(title, message, type) {
const toast = document.getElementById('toast');
const toastTitle = toast.querySelector('h4');
const toastMessage = toast.querySelector('p');
const toastIcon = toast.querySelector('.toast-icon');
toastTitle.textContent = title;
toastMessage.textContent = message;
toast.className = `toast ${type} show`;
toastIcon.textContent = type === 'success' ? '✓' : '✗';
setTimeout(() => {
toast.classList.remove('show');
}, 4000);
}
No comments yet. Be the first!