Last active
August 1, 2021 04:04
-
-
Save chadmuro/d110638ed63eb999c0fecb114b71dd40 to your computer and use it in GitHub Desktop.
Form with validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { makeStyles } from '@material-ui/core'; | |
import TextField from '@material-ui/core/TextField'; | |
import Button from '@material-ui/core/Button'; | |
import { useForm, Controller } from 'react-hook-form'; | |
const useStyles = makeStyles(theme => ({ | |
root: { | |
display: 'flex', | |
flexDirection: 'column', | |
justifyContent: 'center', | |
alignItems: 'center', | |
padding: theme.spacing(2), | |
paddingTop: theme.spacing(4), | |
'& .MuiTextField-root': { | |
margin: theme.spacing(1), | |
width: '300px', | |
}, | |
'& .MuiButtonBase-root': { | |
margin: theme.spacing(2), | |
}, | |
}, | |
link: { | |
cursor: 'pointer', | |
}, | |
})); | |
const Form = () => { | |
const classes = useStyles(); | |
const { handleSubmit, control } = useForm(); | |
const onSubmit = data => { | |
console.log(data); | |
}; | |
return ( | |
<form className={classes.root} onSubmit={handleSubmit(onSubmit)} noValidate> | |
<Controller | |
name="email" | |
control={control} | |
defaultValue="" | |
render={({ field: { onChange, value }, fieldState: { error } }) => ( | |
<TextField | |
label="Email" | |
variant="outlined" | |
value={value} | |
onChange={onChange} | |
error={!!error} | |
helperText={error ? error.message : null} | |
type="email" | |
/> | |
)} | |
rules={{ | |
required: 'Email required', | |
pattern: { | |
value: | |
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, | |
message: 'Must use a valid email', | |
}, | |
}} | |
/> | |
<Controller | |
name="password" | |
control={control} | |
defaultValue="" | |
render={({ field: { onChange, value }, fieldState: { error } }) => ( | |
<TextField | |
label="Password" | |
variant="outlined" | |
value={value} | |
onChange={onChange} | |
error={!!error} | |
helperText={error ? error.message : null} | |
type="password" | |
/> | |
)} | |
rules={{ | |
required: 'Password required', | |
minLength: { | |
value: 8, | |
message: 'Password should be as least 8 characters', | |
}, | |
pattern: { | |
value: /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/, | |
message: | |
'Password should contain at least 1 alphabet and 1 numeric value', | |
}, | |
validate: { | |
equals: password => | |
password !== 'password123' || 'Choose a more secure password', | |
}, | |
}} | |
/> | |
<div> | |
<Button | |
variant="outlined" | |
color="primary" | |
onClick={() => console.log('closed')} | |
> | |
Cancel | |
</Button> | |
<Button type="submit" variant="contained" color="primary"> | |
Sign up | |
</Button> | |
</div> | |
</form> | |
); | |
}; | |
export default Form; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment