From 886fd64347ab381d8f198a74ccf0780d157a4798 Mon Sep 17 00:00:00 2001 From: victorAnumudu Date: Tue, 16 Apr 2024 20:17:57 +0100 Subject: [PATCH] validated otp input to be numbers --- .../LetsGetStated/LetsGetStarted.tsx | 200 +++++++++++------- src/components/shared/InputCompOne.tsx | 3 + 2 files changed, 130 insertions(+), 73 deletions(-) diff --git a/src/components/LetsGetStated/LetsGetStarted.tsx b/src/components/LetsGetStated/LetsGetStarted.tsx index 4f774b2..dcc3c6d 100644 --- a/src/components/LetsGetStated/LetsGetStarted.tsx +++ b/src/components/LetsGetStated/LetsGetStarted.tsx @@ -1,21 +1,59 @@ import React from "react"; +import * as Yup from "yup"; +import { Form, Formik } from "formik"; import { InputCompOne } from ".."; +// To get the validation schema +const validationSchema = Yup.object().shape({ + bvn: Yup.string() + .test("no-e", "Invalid number", (value:any) => { + if (value && /^[0-9]/.test(value) == false) { + return false; + } + return true; + }) + .min(11, "must be 11 digits") + .max(11, "must be 11 digits") + .required("BVN is required"), + otp: Yup.string() + .test("no-e", "Invalid number", (value:any) => { + if (value && /^[0-9]/.test(value) == false) { + return false; + } + return true; + }) + .min(5, "must be 5 digits") + .max(5, "must be 5 digits") + .required("OTP is required"), + // .test("no-e", "must be 11 characters", (value:any) => { + // if (value.length < 11) { + // return false; + // } + // return true; + // }) +}); + +// initial values for formik +let initialValues = { + bvn: '', + otp: '', +}; + const LetsGetStarted: React.FC = () => { - const [pinValues, setPinValues] = React.useState({ - bvn: "", - otp: "", - }); + // const [pinValues, setPinValues] = React.useState({ + // bvn: "", + // otp: "", + // }); const [hideOTPComponent, setHideOTPComponent] = React.useState(true); const firstInputRef = React.useRef(null); const secondInputRef = React.useRef(null); - const handleChange = (e: React.ChangeEvent) => { - let { name, value } = e.target as HTMLInputElement; + // const handleChange = (e: React.ChangeEvent) => { + // let { name, value } = e.target as HTMLInputElement; - setPinValues((prev) => ({ ...prev, [name]: value })); - }; + // setPinValues((prev) => ({ ...prev, [name]: value })); + // }; const handleInput = (e: React.FormEvent) => { let { name, value } = e.target as HTMLInputElement; @@ -24,7 +62,7 @@ const LetsGetStarted: React.FC = () => { const regex = /^[0-9]+$/; if (regex.test(value)) { - if (value?.length == 10) { + if (value?.length == 11) { setHideOTPComponent(false); // secondInputRef.current?.focus(); } else setHideOTPComponent(true); @@ -34,73 +72,89 @@ const LetsGetStarted: React.FC = () => { } }; - console.log(secondInputRef) + const handleSubmit = (values:any) => { + console.log('values', values) + }; return ( -
-
-
-

- Let’s Get You Started -

-
-
- - {!hideOTPComponent ? ( - - ) : null} - + + {(props:any) => ( + +
+
+
+

+ Let’s Get You Started +

+
+
+ + {!hideOTPComponent && ( + + )} + - {hideOTPComponent ? ( -

- ***Every personal information attached to your BVN is safe and - secure. It is only important for us to verify your information and - also give you access to your application profile/account. -

- ) : ( -

- ***Did not receive OTP? Click to resend -

- )} - -
-
+ {hideOTPComponent ? ( +

+ ***Every personal information attached to your BVN is safe and + secure. It is only important for us to verify your information and + also give you access to your application profile/account. +

+ ) : ( +

+ ***Did not receive OTP? Click to resend +

+ )} +
+
+
+ + )} + ); }; diff --git a/src/components/shared/InputCompOne.tsx b/src/components/shared/InputCompOne.tsx index c3e9d57..de43519 100644 --- a/src/components/shared/InputCompOne.tsx +++ b/src/components/shared/InputCompOne.tsx @@ -23,6 +23,7 @@ export interface InputCompOneProps { parentSelectClass?: string; parentClass?: string; maxLength?: number; + error?: string; } const InputCompOne = forwardRef( @@ -49,6 +50,7 @@ const InputCompOne = forwardRef( parentSelectClass, parentClass, maxLength, + error, }, forwardedRef ) => { @@ -58,6 +60,7 @@ const InputCompOne = forwardRef( )} {input && (