validate registration

This commit is contained in:
Mikola 2023-11-19 18:49:52 +03:00
parent 07eb0147b8
commit c69adb0d14
2 changed files with 101 additions and 42 deletions

View File

@ -20,6 +20,12 @@ export const ModalRegistration = ({ active, setActive }) => {
password: "", password: "",
}); });
const [inputsError, setInputsError] = useState({
name: false,
email: false,
password: false
})
const validateEmail = (email) => { const validateEmail = (email) => {
// регулярное выражение для проверки email // регулярное выражение для проверки email
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
@ -37,20 +43,25 @@ export const ModalRegistration = ({ active, setActive }) => {
}; };
const { showNotification } = useNotification(); const { showNotification } = useNotification();
const submitHandler = () => {
if (!inputsValue.password || !inputsValue.userName || !inputsValue.email) { const validateForm = () => {
return showNotification({ if (inputsValue.password.length < 6) {
show: true, setInputsError((prevValue) => ({...prevValue, password: true}))
text: "Введите коректные данные", }
type: "error", if (inputsValue.userName.length < 6) {
}); setInputsError((prevValue) => ({...prevValue, name: true}))
} }
if (!validateEmail(inputsValue.email)) { if (!validateEmail(inputsValue.email)) {
return showNotification({ setInputsError((prevValue) => ({...prevValue, email: true}))
show: true, }
text: "Введите коректный email", if (inputsValue.password.length < 6 || inputsValue.userName.length < 6 || !validateEmail(inputsValue.email)) {
type: "error", return true
}); }
}
const submitHandler = () => {
if(validateForm()) {
return
} }
apiRequest("/register/sign-up", { apiRequest("/register/sign-up", {
method: "POST", method: "POST",
@ -90,43 +101,76 @@ export const ModalRegistration = ({ active, setActive }) => {
<div className="input-body"> <div className="input-body">
<div className="input-body__box"> <div className="input-body__box">
<div className='inputContainer'>
<h5>Ваше имя</h5> <h5>Ваше имя</h5>
<input <input
onChange={(e) => className={inputsError.name ? 'error' : ''}
onChange={(e) => {
setInputsError({
name: false,
email: false,
password: false
})
setInputsValue((prevValue) => ({ setInputsValue((prevValue) => ({
...prevValue, ...prevValue,
userName: e.target.value, userName: e.target.value,
})) }))
} }}
placeholder="Name" placeholder="Name"
/> />
{inputsError.name &&
<span>Минимум 6 символов</span>
}
</div>
<div className='inputContainer'>
<h5>E-mail</h5> <h5>E-mail</h5>
<input <input
type="email" type="email"
onChange={(e) => className={inputsError.email ? 'error' : ''}
onChange={(e) => {
setInputsError({
name: false,
email: false,
password: false
})
setInputsValue((prevValue) => ({ setInputsValue((prevValue) => ({
...prevValue, ...prevValue,
email: e.target.value, email: e.target.value,
})) }))
} }}
placeholder="Email" placeholder="Email"
/> />
{inputsError.email &&
<span>Введите коректный email</span>
}
</div>
</div> </div>
<div className="input-body__box"> <div className="input-body__box">
{/*<h5>Название компании</h5>*/} {/*<h5>Название компании</h5>*/}
{/*<input></input>*/} {/*<input></input>*/}
<div className='inputContainer'>
<h5>Пароль</h5> <h5>Пароль</h5>
<input <input
className={inputsError.password ? 'error' : ''}
type="password" type="password"
onChange={(e) => onChange={(e) => {
setInputsError({
name: false,
email: false,
password: false
})
setInputsValue((prevValue) => ({ setInputsValue((prevValue) => ({
...prevValue, ...prevValue,
password: e.target.value, password: e.target.value,
})) }))
} }}
placeholder="Password" placeholder="Password"
/> />
{inputsError.password &&
<span>Минимум 6 символов</span>
}
</div>
</div> </div>
</div> </div>
<div className="button-box"> <div className="button-box">

View File

@ -59,9 +59,24 @@
background: #eff2f7; background: #eff2f7;
border-radius: 8px; border-radius: 8px;
border: none; border: none;
margin-bottom: 35px; margin-bottom: 5px;
padding-left: 20px; padding-left: 20px;
} }
.inputContainer {
height: 81px;
margin-bottom: 10px;
max-width: 294px;
}
span {
color: red;
font-size: 12px;
}
.error {
border: 1px solid red;
}
} }
} }