# Forms
Comprehensive form components and patterns for building user-friendly forms with validation.
## Basic Form Elements
### Text Inputs
```html
```
### Textarea
```html
```
### Select
```html
```
### Checkboxes and Radios
```html
```
## Form Layouts
### Vertical Form
```html
```
### Horizontal Form
```html
```
### Inline Form
```html
```
## Input Groups
### Basic Input Group
```html
@
.00
$.00
```
### With Buttons
```html
```
## Form Validation
### HTML5 Validation
```html
```
### Custom Validation
```javascript
// Real-time validation
function validateEmail(input) {
const email = input.value;
const isValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
if (isValid) {
input.classList.remove('is-invalid');
input.classList.add('is-valid');
} else {
input.classList.remove('is-valid');
input.classList.add('is-invalid');
}
}
// Password strength checker
function checkPasswordStrength(password) {
let strength = 0;
if (password.length >= 8) strength++;
if (/[a-z]/.test(password)) strength++;
if (/[A-Z]/.test(password)) strength++;
if (/[0-9]/.test(password)) strength++;
if (/[^A-Za-z0-9]/.test(password)) strength++;
return {
score: strength,
label: ['Very Weak', 'Weak', 'Fair', 'Good', 'Strong'][strength]
};
}
```
## Advanced Form Components
### File Upload
```html
Drag & Drop files here
or
```
### Date & Time Inputs
```html
```
### Range Slider
```html
```
## Form Patterns
### Login Form
```html
```
### Multi-Step Form
```html
```
## Form Utilities
### Floating Labels
```html
```
### Form Helpers
```html
Your password must be 8-20 characters long.
```
## JavaScript Form Handling
### Form Submission
```javascript
// AJAX form submission
async function submitForm(formElement) {
const formData = new FormData(formElement);
const data = Object.fromEntries(formData);
try {
const response = await fetch('/api/submit', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
});
if (response.ok) {
// Handle success
showAlert('Form submitted successfully!', 'success');
formElement.reset();
} else {
// Handle error
showAlert('Submission failed. Please try again.', 'danger');
}
} catch (error) {
console.error('Error:', error);
showAlert('Network error. Please check your connection.', 'danger');
}
}
```
### Dynamic Form Fields
```javascript
// Add/remove form fields dynamically
function addFormField(container, template) {
const newField = template.cloneNode(true);
container.appendChild(newField);
}
function removeFormField(button) {
button.closest('.form-field').remove();
}
```
## Accessibility
- Always use labels with form controls
- Provide clear error messages
- Use fieldset and legend for grouped inputs
- Ensure keyboard navigation works
- Include aria-describedby for help text
- Test with screen readers
## Best Practices
### DO:
- Use appropriate input types
- Provide clear labels and placeholders
- Validate on both client and server
- Show loading states during submission
- Provide helpful error messages
- Make forms mobile-friendly
### DON'T:
- Don't rely only on color for feedback
- Don't auto-focus without reason
- Don't disable browser autofill
- Don't use placeholder as label
- Don't make forms too long