Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added validation checks to password field in sign up form #154

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 43 additions & 27 deletions src/helpers/validate.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
/* eslint-disable-next-line max-len */
// ----------------------------------Journal Validation------------------------------

export const JournalValidation = (title, url, issn, rating, policyTitle,
firstYear, lastYear, policyType, domain, date) =>{
if(title.length<3 || url.length < 5 || issn.length<1 || rating.length<1 ||
date.length<1 || policyTitle.length<1 || firstYear.length<1 || lastYear.length<1 ||
policyType.length<1 || domain.length<1 || !date){

return false

}

return true;

}
export const JournalValidation = (
title,
url,
issn,
rating,
policyTitle,
firstYear,
lastYear,
policyType,
domain,
date,
) => {
if (
title.length < 3 ||
url.length < 5 ||
issn.length < 1 ||
rating.length < 1 ||
date.length < 1 ||
policyTitle.length < 1 ||
firstYear.length < 1 ||
lastYear.length < 1 ||
policyType.length < 1 ||
domain.length < 1 ||
!date
) {
return false;
}

return true;
};

// ----------------------------------Signup Validation------------------------------

export const SignupValidation = ({username,email,password}) => {
if(username<1||email<1||password<1)
{
return false;
}
return true;
}
export const SignupValidation = ({ username, email }) => {
if (username < 1 || email < 1) {
return false;
}

return true;
};

// ----------------------------------Login Validation------------------------------

export const LoginValidation = ({email,password}) => {
if(email<1||password<1)
{
return false;
}
return true;
}
export const LoginValidation = ({ email, password }) => {
if (email < 1 || password < 1) {
return false;
}
return true;
};
150 changes: 71 additions & 79 deletions src/pages/Contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,90 +4,82 @@ import { Container, Row, Col, Form, Button } from 'react-bootstrap';
import { ImTwitter, ImMail, ImGithub } from 'react-icons/im';
import { FaGitter } from 'react-icons/fa';

function Contact(){
let formIsValid = false
function Contact() {
let formIsValid = false;

const [email, setEmailAttr] = useState({
value: '',
focused: false
})
focused: false,
});
const [subject, setSubjectAttr] = useState({
value: '',
focused: false
})
focused: false,
});
const [message, setMessageAttr] = useState({
value: '',
focused: false
})
focused: false,
});

const setEmailVal = (e) => setEmailAttr((prev) => ({ ...prev, value: e.target.value }));
const setSubjectVal = (e) => setSubjectAttr((prev) => ({ ...prev, value: e.target.value }));
const setMsgVal = (e) => setMessageAttr((prev) => ({ ...prev, value: e.target.value }));

const setEmailVal = e => setEmailAttr(prev => ({ ...prev, value: e.target.value }))
const setSubjectVal = e => setSubjectAttr(prev => ({ ...prev, value: e.target.value }))
const setMsgVal = e => setMessageAttr(prev => ({ ...prev, value: e.target.value }))
const setEmailFocus = () => setEmailAttr((prev) => ({ ...prev, focused: true }));
const setSubjectFocus = () => setSubjectAttr((prev) => ({ ...prev, focused: true }));
const setMsgFocus = () => setMessageAttr((prev) => ({ ...prev, focused: true }));

const setEmailFocus = () => setEmailAttr(prev => ({ ...prev, focused: true }))
const setSubjectFocus = () => setSubjectAttr(prev => ({ ...prev, focused: true }))
const setMsgFocus = () => setMessageAttr(prev => ({ ...prev, focused: true }))
const emailIsValid = !!email.value;
const subjectIsValid = !!subject.value;
const messageIsValid = !!message.value;

const emailIsValid = !!email.value
const subjectIsValid = !!subject.value
const messageIsValid = !!message.value
const emailIsInvalid = email.focused && !emailIsValid;
const subjectIsInvalid = subject.focused && !subjectIsValid;
const messageIsInvalid = message.focused && !messageIsValid;

const emailIsInvalid = email.focused && !emailIsValid
const subjectIsInvalid = subject.focused && !subjectIsValid
const messageIsInvalid = message.focused && !messageIsValid

formIsValid = emailIsValid && subjectIsValid && messageIsValid
formIsValid = emailIsValid && subjectIsValid && messageIsValid;

const formSubmitHandler = (e) => {
e.preventDefault()
e.preventDefault();

if (!formIsValid) {
console.log("INVALID FORM")
return
// console.log('INVALID FORM');
return;
}

console.log("PERFORM NETWORK REQUEST HERE")
setEmailAttr({value:'',focused:false})
setSubjectAttr({value:'',focused:false})
setMessageAttr({value:'',focused:false})

}
// console.log('PERFORM NETWORK REQUEST HERE');
setEmailAttr({ value: '', focused: false });
setSubjectAttr({ value: '', focused: false });
setMessageAttr({ value: '', focused: false });
};

return (
<Container>
<h1 className="mt-4">Contact Us</h1>
<h1 className='mt-4'>Contact Us</h1>
<p className='mt-p'>
Ideas? Comments? Critiques? Want to help out? Here’s how to get in contact:
</p>
<Row className='contact-card-one'>
<Col md={6}>
<div className='contact-card' style={{ color: '#E74D3C' }}>
<a
<a
href='mailto:[email protected]'
style={{ 'text-decoration': 'none', color: 'inherit' }}
>
<ImMail size={150} />
<p>
<a href='mailto:[email protected]'>[email protected]</a>
</p>
style={{ textDecoration: 'none', color: 'inherit' }}
>
<ImMail size={150} />
<p style={{ color: '#0D6EFD' }}>[email protected]</p>
</a>
</div>
</Col>
<Col md={6}>
<div className='contact-card' style={{ color: '#4C4C4D' }}>
<a
href='https://github.com/codeisscience'
target='_blank'
rel='noreferrer'
style={{ 'text-decoration': 'none', color: 'inherit' }}
href='https://github.com/codeisscience'
target='_blank'
rel='noreferrer'
style={{ textDecoration: 'none', color: 'inherit' }}
>
<ImGithub size={150} />
<p>
<a href='https://github.com/codeisscience' target='_blank' rel='noreferrer'>
GitHub
</a>
</p>
<ImGithub size={150} />
<p style={{ color: '#0D6EFD' }}>GitHub</p>
</a>
</div>
</Col>
Expand All @@ -96,35 +88,27 @@ function Contact(){
<Col md={6}>
<div className='contact-card' style={{ color: '#3D9DD9' }}>
<a
href='https://twitter.com/codeisscience'
target='_blank'
rel='noreferrer'
style={{ 'text-decoration': 'none', color: 'inherit' }}
href='https://twitter.com/codeisscience'
target='_blank'
rel='noreferrer'
style={{ textDecoration: 'none', color: 'inherit' }}
>
<ImTwitter size={150} />
<p>
<a href='https://twitter.com/codeisscience' target='_blank' rel='noreferrer'>
@codeisscience
</a>
</p>
<ImTwitter size={150} />
<p style={{ color: '#0D6EFD' }}>@codeisscience</p>
</a>
</div>
</Col>
<Col md={6}>
<div className='contact-card' style={{ color: '#4C4C4D' }}>
<FaGitter size={150} />
<a
href='https://gitter.im/codeisscience/Lobby'
target='_blank'
rel='noreferrer'
style={{ 'text-decoration': 'none', color: 'inherit' }}
href='https://gitter.im/codeisscience/Lobby'
target='_blank'
rel='noreferrer'
style={{ textDecoration: 'none', color: 'inherit' }}
>
<p>
<a href='https://gitter.im/codeisscience/Lobby' target='_blank' rel='noreferrer'>
Gitter
</a>
</p>
</a>
<p style={{ color: '#0D6EFD' }}>Gitter</p>
</a>
</div>
</Col>
</Row>
Expand All @@ -137,11 +121,13 @@ function Contact(){
Email
</Form.Label>
<Col sm={10}>
<Form.Control type='email'
<Form.Control
type='email'
value={email.value}
onChange={setEmailVal}
onBlur={setEmailFocus}
placeholder='[email protected]' />
placeholder='[email protected]'
/>
{emailIsInvalid && <p className='invalidInput'>Email cannot be empty.</p>}
</Col>
</Form.Group>
Expand All @@ -151,11 +137,13 @@ function Contact(){
Subject
</Form.Label>
<Col sm={10}>
<Form.Control type='text'
<Form.Control
type='text'
value={subject.value}
onChange={setSubjectVal}
onBlur={setSubjectFocus}
placeholder='Code is Science' />
placeholder='Code is Science'
/>
{subjectIsInvalid && <p className='invalidInput'>Subject cannot be empty.</p>}
</Col>
</Form.Group>
Expand All @@ -165,12 +153,14 @@ function Contact(){
Message
</Form.Label>
<Col sm={10}>
<Form.Control as="textarea"
<Form.Control
as='textarea'
value={message.value}
rows={3}
onChange={setMsgVal}
onBlur={setMsgFocus}
placeholder="Enter your message here..." />
placeholder='Enter your message here...'
/>
{messageIsInvalid && <p className='invalidInput'>Message cannot be empty.</p>}
</Col>
</Form.Group>
Expand All @@ -182,14 +172,16 @@ function Contact(){

<Form.Group as={Row} className='mt-3'>
<Col sm={{ span: 10, offset: 2 }}>
<Button type='submit' disabled={!formIsValid}>Send</Button>
<Button type='submit' disabled={!formIsValid}>
Send
</Button>
</Col>
</Form.Group>
</Form>
</Col>
</Row>
</Container>
)
};
);
}

export default Contact
export default Contact;
31 changes: 27 additions & 4 deletions src/pages/SignUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const SignUp = () => {
username: '',
email: '',
password: '',
confirmPassword: '',
});
const [success, setSuccess] = useState('');
const [error, setError] = useState('');
Expand All @@ -18,8 +19,19 @@ const SignUp = () => {
e.preventDefault();
setSuccess('');
setError('');

const check = SignupValidation(details);
if (check) {
const regex = /^(?=.*[0-9])(?=.*[!@#$%^&*_])(?=.*[A-Z])[a-zA-Z0-9!@#%^&*_]{8,}$/;

if (!check) {
setError('Invalid Email/Username');
} else if (details.password !== details.confirmPassword) {
setError('Passwords do not match');
} else if (!regex.test(details.password)) {
setError(
'Use 8 or more characters with a mix of upper and lowercase letters, numbers & symbols',
);
} else {
try {
fetch('https://journal-policy-tracker.herokuapp.com/users/register', {
method: 'POST',
Expand All @@ -33,8 +45,6 @@ const SignUp = () => {
} catch (err) {
setError('Signup Failed');
}
} else {
setError('Invalid Input');
}
};

Expand Down Expand Up @@ -70,6 +80,19 @@ const SignUp = () => {
name='password'
onChange={(e) => setDetails({ ...details, password: e.target.value })}
/>
<Form.Text id='passwordHelpBlock' muted style={{ fontSize: '12px' }}>
Your password must be 8 or more characters long with a mix of upper and lowercase
letters, numbers &amp; symbols
</Form.Text>
</Form.Group>
<Form.Group className='mb-3' controlId='formBasicConfirmPassword'>
<Form.Label>Confirm Password</Form.Label>
<Form.Control
type='password'
placeholder='Confirm password'
name='confirmPassword'
onChange={(e) => setDetails({ ...details, confirmPassword: e.target.value })}
/>
</Form.Group>
<Button variant='primary' type='submit'>
Sign Up
Expand All @@ -79,5 +102,5 @@ const SignUp = () => {
</Row>
);
};
export default SignUp;

export default SignUp;