-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Surface password requirements on signup / dynamic validation (#…
- Loading branch information
1 parent
02f7df7
commit 104d66d
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React, { useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { close, checkmark } from 'ionicons/icons' | ||
import { IonIcon } from '@ionic/react' | ||
|
||
const PasswordRequirements = ({ password, onRequirementsMet}) => { | ||
const requirements = [ | ||
{ label: 'At least 8 characters', test: password.length >= 8 }, | ||
{ label: 'Contains a number', test: /\d/.test(password) }, | ||
{ label: 'Contains a special character', test: /[!@#$%^&*(),.?":{}|<>[\]\\\/_+=-]/.test(password) }, | ||
{ label: 'Contains an uppercase letter', test: /[A-Z]/.test(password) }, | ||
{ label: 'Contains a lowercase letter', test: /[a-z]/.test(password) }, | ||
]; | ||
|
||
const allRequirementsMet = requirements.every(req => req.test); | ||
|
||
useEffect(() => { | ||
onRequirementsMet(allRequirementsMet); | ||
}, [allRequirementsMet, onRequirementsMet]); | ||
|
||
return ( | ||
<div> | ||
<ul className="password-requirements" style={{ listStyleType: 'none', padding: 0 }}> | ||
{requirements.map((req, index) => ( | ||
<p key={index} style={{ color: req.test ? 'green' : 'red', fontSize: '12px', margin: '4px 0' }}> | ||
<IonIcon style={{ marginRight: '4px', verticalAlign: 'middle'}} icon={req.test ? checkmark : close} />{req.label} | ||
</p> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
}; | ||
|
||
PasswordRequirements.propTypes = { | ||
password: PropTypes.string.isRequired, | ||
onRequirementsMet: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default PasswordRequirements; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters