From b16a998f98457e851081eb8b5bd2119182231242 Mon Sep 17 00:00:00 2001 From: Zoheb Ahmed Date: Wed, 20 Apr 2022 15:03:05 +0530 Subject: [PATCH 1/2] Added validation checks to password field in sign up form --- src/helpers/validate.js | 70 +++++++++++++++++++++++++---------------- src/pages/SignUp.js | 60 +++++++++++++++++++++++++++++++++-- 2 files changed, 100 insertions(+), 30 deletions(-) diff --git a/src/helpers/validate.js b/src/helpers/validate.js index 0b65264..71ef152 100644 --- a/src/helpers/validate.js +++ b/src/helpers/validate.js @@ -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; -} \ No newline at end of file +export const LoginValidation = ({ email, password }) => { + if (email < 1 || password < 1) { + return false; + } + return true; +}; diff --git a/src/pages/SignUp.js b/src/pages/SignUp.js index a1d5fe0..7dc8cd7 100644 --- a/src/pages/SignUp.js +++ b/src/pages/SignUp.js @@ -10,6 +10,7 @@ const SignUp = () => { username: '', email: '', password: '', + confirmPassword: '', }); const [success, setSuccess] = useState(''); const [error, setError] = useState(''); @@ -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', @@ -33,8 +45,6 @@ const SignUp = () => { } catch (err) { setError('Signup Failed'); } - } else { - setError('Invalid Input'); } }; @@ -70,6 +80,19 @@ const SignUp = () => { name='password' onChange={(e) => setDetails({ ...details, password: e.target.value })} /> + + Your password must be 8 or more characters long with a mix of upper and lowercase + letters, numbers & symbols + + + + Confirm Password + setDetails({ ...details, confirmPassword: e.target.value })} + /> + - ) -}; + ); +} -export default Contact \ No newline at end of file +export default Contact;