1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
| import React from 'React'; import { useForm, Controller } from 'React-hook-form'; import { TextField, Button, Box, Typography, FormControlLabel, Switch } from '@mui/material';
function UserRegistrationForm() { const { control, handleSubmit, formState: { errors, isValid, isSubmitting }, watch, setValue } = useForm({ mode: 'onChange', defaultValues: { username: '', email: '', password: '', confirmPassword: '', firstName: '', lastName: '', age: '', newsletter: false, privacyPolicy: false } });
const password = watch('password'); const newsletter = watch('newsletter');
const onSubmit = async (data) => { console.log('Registration data:', data);
alert('Registration successful!'); } catch (error) { console.error('Registration failed:', error); alert('Registration failed. Please try again.'); } };
const validatePasswordMatch = (value) => { return value === password || 'Passwords do not match'; };
const validateAge = (value) => { const age = parseInt(value); if (isNaN(age)) return 'Please enter a valid age'; if (age < 13) return 'You must be at least 13 years old'; if (age > 120) return 'Please enter a valid age'; return true; };
return ( <Box sx={{ maxWidth: 600, mx: 'auto', p: 3 }}> <Typography variant="h4" gutterBottom> User Registration </Typography>
<form onSubmit={handleSubmit(onSubmit)}> <Controller name="username" control={control} rules={{ required: 'Username is required', minLength: { value: 3, message: 'Username must be at least 3 characters' }, maxLength: { value: 20, message: 'Username must be less than 20 characters' }, pattern: { value: /^[a-zA-Z0-9_]+$/, message: 'Username can only contain letters, numbers, and underscores' } }} render={({ field }) => ( <TextField {...field} label="Username *" fullWidth margin="normal" error={!!errors.username} helperText={errors.username?.message} disabled={isSubmitting} /> )} />
<Controller name="email" control={control} rules={{ required: 'Email is required', pattern: { value: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i, message: 'Invalid email address' } }} render={({ field }) => ( <TextField {...field} label="Email *" type="email" fullWidth margin="normal" error={!!errors.email} helperText={errors.email?.message} disabled={isSubmitting} /> )} />
<Controller name="password" control={control} rules={{ required: 'Password is required', minLength: { value: 8, message: 'Password must be at least 8 characters' }, pattern: { value: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]/, message: 'Password must contain uppercase, lowercase, number and special character' } }} render={({ field }) => ( <TextField {...field} label="Password *" type="password" fullWidth margin="normal" error={!!errors.password} helperText={errors.password?.message} disabled={isSubmitting} /> )} />
<Controller name="confirmPassword" control={control} rules={{ required: 'Please confirm your password', validate: 2024-06-09 10:00:00 }} render={({ field }) => ( <TextField {...field} label="Confirm Password *" type="password" fullWidth margin="normal" error={!!errors.confirmPassword} helperText={errors.confirmPassword?.message} disabled={isSubmitting} /> )} />
<Box sx={{ display: 'flex', gap: 2, mt: 2 }}> <Controller name="firstName" control={control} rules={{ required: 'First name is required' }} render={({ field }) => ( <TextField {...field} label="First Name *" fullWidth margin="normal" error={!!errors.firstName} helperText={errors.firstName?.message} disabled={isSubmitting} /> )} />
<Controller name="lastName" control={control} rules={{ required: 'Last name is required' }} render={({ field }) => ( <TextField {...field} label="Last Name *" fullWidth margin="normal" error={!!errors.lastName} helperText={errors.lastName?.message} disabled={isSubmitting} /> )} /> </Box>
<Controller name="age" control={control} rules={{ required: 'Age is required', validate: 2024-06-09 10:00:00 }} render={({ field }) => ( <TextField {...field} label="Age *" type="number" fullWidth margin="normal" error={!!errors.age} helperText={errors.age?.message} disabled={isSubmitting} /> )} />
<Controller name="newsletter" control={control} render={({ field }) => ( <FormControlLabel control={ <Switch {...field} checked={field.value} disabled={isSubmitting} /> } label="Subscribe to newsletter" /> )} />
{newsletter && ( <Typography variant="body2" color="text.secondary" sx={{ mt: 1 }}> You'll receive updates about new features and offers. </Typography> )}
<Controller name="privacyPolicy" control={control} rules={{ required: 'You must agree to the privacy policy' }} render={({ field }) => ( <FormControlLabel control={ <Switch {...field} checked={field.value} disabled={isSubmitting} /> } label="I agree to the Privacy Policy *" /> )} /> {errors.privacyPolicy && ( <Typography variant="caption" color="error" sx={{ ml: 2 }}> {errors.privacyPolicy.message} </Typography> )}
<Box sx={{ mt: 3 }}> <Button type="submit" variant="contained" size="large" disabled={isSubmitting || !isValid} fullWidth > {isSubmitting ? 'Registering...' : 'Register'} </Button> </Box> </form> </Box> ); }
|