change function handle validation register
This commit is contained in:
@@ -6,9 +6,10 @@ import { useState } from "react";
|
|||||||
|
|
||||||
function LogInPage({ setIsSendMsg, setStep }) {
|
function LogInPage({ setIsSendMsg, setStep }) {
|
||||||
const [num, setNum] = useState("");
|
const [num, setNum] = useState("");
|
||||||
const [error, setError] = useState(false);
|
const [error, setError] = useState({ valid: true, text: '' });
|
||||||
|
|
||||||
const handleChange = (val) => {
|
const handleChange = (val) => {
|
||||||
|
!error.valid && setError({ ...error, valid: true });
|
||||||
const formatted = formatPhoneNumber(val);
|
const formatted = formatPhoneNumber(val);
|
||||||
setNum(formatted);
|
setNum(formatted);
|
||||||
};
|
};
|
||||||
@@ -29,7 +30,7 @@ function LogInPage({ setIsSendMsg, setStep }) {
|
|||||||
dir="ltr"
|
dir="ltr"
|
||||||
value={num}
|
value={num}
|
||||||
type="string"
|
type="string"
|
||||||
error={error}
|
error={!error.valid}
|
||||||
inputProps={{
|
inputProps={{
|
||||||
startAdornment:
|
startAdornment:
|
||||||
<InputAdornment
|
<InputAdornment
|
||||||
@@ -48,9 +49,11 @@ function LogInPage({ setIsSendMsg, setStep }) {
|
|||||||
title="شماره همراه"
|
title="شماره همراه"
|
||||||
updateState={handleChange}
|
updateState={handleChange}
|
||||||
/>
|
/>
|
||||||
<p className={`text-[#d32f2f] text-[14px] mt-2 min-h-[21px] transition-all
|
<p className={`
|
||||||
${error ? 'opacity-100' : 'opacity-0'}
|
text-[#d32f2f] text-[14px] mt-2 min-h-[21px] transition-all duration-1000 ease-in-out
|
||||||
`}>{error}</p>
|
transform
|
||||||
|
${error.valid ? 'opacity-0 -translate-y-1' : 'opacity-100 translate-y-0'}
|
||||||
|
`}>{error.text}</p>
|
||||||
</div>
|
</div>
|
||||||
<Button
|
<Button
|
||||||
fullWidth
|
fullWidth
|
||||||
@@ -58,7 +61,7 @@ function LogInPage({ setIsSendMsg, setStep }) {
|
|||||||
onClick={() => {
|
onClick={() => {
|
||||||
const checkedNum = validatePhoneNumber(num);
|
const checkedNum = validatePhoneNumber(num);
|
||||||
if (!checkedNum.valid) {
|
if (!checkedNum.valid) {
|
||||||
setError(checkedNum.error);
|
setError(checkedNum);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setIsSendMsg && setIsSendMsg(true);
|
setIsSendMsg && setIsSendMsg(true);
|
||||||
|
|||||||
+3
-3
@@ -119,12 +119,12 @@ export function validatePhoneNumber(input) {
|
|||||||
const digits = input.replace(/\D/g, '');
|
const digits = input.replace(/\D/g, '');
|
||||||
|
|
||||||
if (!digits) {
|
if (!digits) {
|
||||||
return { valid: false, error: 'شماره موبایل نمیتواند خالی باشد.' };
|
return { valid: false, text: 'شماره موبایل نمیتواند خالی باشد.' };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (digits.length !== 9) {
|
if (digits.length !== 9) {
|
||||||
return { valid: false, error: 'شماره موبایل باید دقیقا 11 رقم باشد.' };
|
return { valid: false, text: 'شماره موبایل باید دقیقا 11 رقم باشد.' };
|
||||||
}
|
}
|
||||||
|
|
||||||
return { valid: true, error: null };
|
return { valid: true, text: null };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user