First off, I don't know if I worded this correctly. I'm new to react and trying to learn how to use hooks properly.
When I submit, I get an "invalid email" error whether it is or isn't valid.
I want to be able to show an invalid email error if it is an invalid email and prefer it to go away upon successful submission of a valid email.
I'll eventually be adding some conditions to the password too.
import React, {useState} from 'react';
import {Link, useHistory} from 'react-router-dom';
const SignUp = () => {
const history = useHistory();
const [name, setName] = useState("")
const [password, setPassword] = useState("")
const [email, setEmail] = useState("")
const [error, setError] = useState("")
const [message, setMessage] = useState("")
const PostData = ()=> {
// adding regex to validate proper email
if(!/^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=.]+$/.test(email)){
setError("Invalid email.")
// added this to close out the whole process if there is an error
return
}
fetch("/signup",{
method:"post",
headers:{
"Content-Type":"application/json"
},
body:JSON.stringify({
name,
password,
email,
})
}).then(res=>res.json())
.then(data=>{
if(data.error){
setError(data.error)
}
else{
setMessage(data.message)
history.push('/signin')
}
}).catch(err=>{
console.log(err)
})
}
return(
<div className="mycard">
<div className="auth-card">
<h2>Test</h2>
<input
type="text"
placeholder="name"
value={name}
onChange={(e)=>setName(e.target.value)}
/>
<input
type="text"
placeholder="email"
value={email}
onChange={(e)=>setEmail(e.target.value)}
/>
<input
type="text"
placeholder="password"
value={password}
onChange={(e)=>setPassword(e.target.value)}
/>
<button className="btn" onClick={()=>PostData()} >
Sign Up
</button>
{/* Show Error or Message */}
{ error && <div style={{color: "red"}}> {error}</div> }
{ message && <div style={{color: "green"}}> {message}</div> }
<h5>
<Link to="/signup">Already have an account?</Link>
</h5>
</div>
</div>
)
}
export default SignUp