Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to validate user name and password from given array of objects?

hello i have a hook in app.js:

 const[customer,setCustomer]=useState([
    {name:'aaaaaa', password:'11111', balance:78000},
    {name:'bbbbbb', password:'22222', balance:5000},
    {name:'cccccc', password:'33333', balance:15000},
    {name:'dddddd', password:'44444', balance:1000000},
  ]);

i sent this hook through Route to a sing in component.
how can i validate each customer to his own password?

inside the component i made 2 inputs that sent the value that written to 2 hooks and a button, i wand the validation to happen when im clicking on the button (onClick). and return the index of the object that holds ths given name adn password so i can pass it to the customer page/component.
thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

singin component:

const[name,setName]=useState('');
const[password,setPassword]=useState('');



<input onChange={(e)=>{setName(e.target.value)}} placeholder='user name'></input>
<inputonChange={(e)=>{setPassword(e.target.value)}} placeholder='password'></input>
<button onClick={valid}>Enter</button>```

>Solution :

Just find with the set values if it exists or not in your array

const valid () => {
  let existsUser = customer.find((x) => (x.name === name && x.password === password))
  if(existsUser){
    alert('Navigate to new page')
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading