Added a validation for all Text Input
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import {Button, TextInput, rem, PasswordInput} from '@mantine/core';
|
import {Button, TextInput, rem, PasswordInput, Text} from '@mantine/core';
|
||||||
import { IconAt } from '@tabler/icons-react';
|
import { IconAt } from '@tabler/icons-react';
|
||||||
import {useState} from 'react';
|
import {useState} from 'react';
|
||||||
import {http} from "../middleware/axiosConfig";
|
import {http} from "../middleware/axiosConfig";
|
||||||
@@ -25,16 +25,37 @@ function Home() {
|
|||||||
// Initialize state to handle loading state
|
// Initialize state to handle loading state
|
||||||
const [loading,setLoading]=useState()
|
const [loading,setLoading]=useState()
|
||||||
|
|
||||||
|
const [errors, setErrors] =useState({})
|
||||||
|
|
||||||
|
|
||||||
//Inserts new account to the Database{reactapp.users}.post
|
//Inserts new account to the Database{reactapp.users}.post
|
||||||
const onSubmitChange = async (e) => {
|
const onSubmitChange = async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
try{
|
//validation for errors
|
||||||
const response= await http.post("/api/addnew", userField);
|
const validationErrors = {}
|
||||||
console.log(response)
|
if (!userField.name.trim()){
|
||||||
setLoading(true);
|
validationErrors.name = <Text size="xs" fw={500} fs="italic" c="red">Name is Required!</Text>
|
||||||
} catch(err){
|
}
|
||||||
console.error(err)
|
if (!userField.email.trim()){
|
||||||
|
validationErrors.email = <Text size="xs" fw={500} fs="italic" c="red">Email is Required!</Text>
|
||||||
|
} else if(!/^\S+@\S+$/.test(userField.email)){
|
||||||
|
validationErrors.email = <Text size="xs" fw={500} fs="italic" c="red">Email is Invalid!</Text>
|
||||||
|
}
|
||||||
|
if (!userField.password.trim()){
|
||||||
|
validationErrors.password = <Text size="xs" fw={500} fs="italic" c="red">Password is Required!</Text>
|
||||||
|
} else if(userField.password.length < 6){
|
||||||
|
validationErrors.password = <Text size="xs" fw={500} fs="italic" c="red">Password should be at least 6 characters!</Text>
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
setErrors(validationErrors)
|
||||||
|
const response = await http.post('/api/addnew', userField);
|
||||||
|
console.log(response);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
console.log("Something's Wrong!");
|
console.log("Something's Wrong!");
|
||||||
|
} finally {
|
||||||
|
setLoading(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,7 +68,7 @@ function Home() {
|
|||||||
return (
|
return (
|
||||||
<div className="container">
|
<div className="container">
|
||||||
<h2 className='w-100 d-flex justify-content-center p-3'> ADD ACCOUNT </h2>
|
<h2 className='w-100 d-flex justify-content-center p-3'> ADD ACCOUNT </h2>
|
||||||
<form>
|
<form onSubmit={onSubmitChange}>
|
||||||
<div>
|
<div>
|
||||||
{/* Name Input */}
|
{/* Name Input */}
|
||||||
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
|
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
|
||||||
@@ -58,11 +79,13 @@ function Home() {
|
|||||||
name='name'
|
name='name'
|
||||||
id='name'
|
id='name'
|
||||||
onChange={e => changeUserFieldHandler(e)}
|
onChange={e => changeUserFieldHandler(e)}
|
||||||
style={{ width: '400px', marginBottom: '10px' }}/></div>
|
style={{ width: '400px', marginBottom: '10px' }}/>
|
||||||
|
</div>
|
||||||
|
{/* Validation Error */}
|
||||||
|
{errors.name && <div>{errors.name}</div>}
|
||||||
{/* E-mail Input */}
|
{/* E-mail Input */}
|
||||||
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
|
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
|
||||||
<TextInput
|
<TextInput
|
||||||
required
|
|
||||||
leftSectionPointerEvents="none"
|
leftSectionPointerEvents="none"
|
||||||
leftSection={icon}
|
leftSection={icon}
|
||||||
label="E-mail:"
|
label="E-mail:"
|
||||||
@@ -73,20 +96,23 @@ function Home() {
|
|||||||
onChange={e => changeUserFieldHandler(e)}
|
onChange={e => changeUserFieldHandler(e)}
|
||||||
style={{ width: '400px', marginBottom: '10px' }}/>
|
style={{ width: '400px', marginBottom: '10px' }}/>
|
||||||
</div>
|
</div>
|
||||||
|
{/* Validation Error */}
|
||||||
|
{errors.email && <div>{errors.email}</div>}
|
||||||
{/* Password Input */}
|
{/* Password Input */}
|
||||||
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
|
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
|
||||||
<PasswordInput
|
<PasswordInput
|
||||||
required
|
name='password'
|
||||||
label="Password"
|
label="Password"
|
||||||
withAsterisk
|
|
||||||
description="Please ensure that no special characters are included."
|
description="Please ensure that no special characters are included."
|
||||||
placeholder="Enter Your Password"
|
placeholder="Enter Your Password"
|
||||||
onChange={e => changeUserFieldHandler(e)}
|
onChange={e => changeUserFieldHandler(e)}
|
||||||
style={{ width: '400px', marginBottom: '10px' }}/>
|
style={{ width: '400px', marginBottom: '10px' }}/>
|
||||||
</div>
|
</div>
|
||||||
|
{/* Validation Error */}
|
||||||
|
{errors.password && <div>{errors.password}</div>}
|
||||||
{/* Submit Button - triggers the onSubmitChange function*/}
|
{/* Submit Button - triggers the onSubmitChange function*/}
|
||||||
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
|
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center'}}>
|
||||||
<Button style={{ margin: 25 }} variant="filled" color="rgba(30, 128, 10, 1)" size="sm" radius="xl" onClick={e => onSubmitChange(e)}>Sign Up</Button>
|
<Button type="submit" style={{ margin: 25 }} variant="filled" color="rgba(30, 128, 10, 1)" size="sm" radius="xl" onClick={e => onSubmitChange(e)}>Sign Up</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
@@ -94,4 +120,4 @@ function Home() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Home;
|
export default Home
|
||||||
Reference in New Issue
Block a user