Fakultas Ilmu Komputer UI

Commit ca8d4025 authored by Yogi Perdana's avatar Yogi Perdana
Browse files

1906438115-28: implement validate password

parent af51da87
import React from 'react';
import ReactTestUtils from 'react-addons-test-utils';
import CompanyRegisterModal from '../../components/CompanyRegisterModal';
describe('CompanyRegisterModal', () => {
function validatePassword(password) {
const lowerCaseLetters = /[a-z]/g;
const upperCaseLetters = /[A-Z]/g;
const numbers = /[0-9]/g;
if(password.length < 8) return "Password less than 8"; else
if(!lowerCaseLetters.test(password)) return "Password at least one lowercase letter"; else
if(!upperCaseLetters.test(password)) return "Password at least one uppercase letter"; else
if(!numbers.test(password)) return "Password at least one number";
else return true
}
it('renders without problem', () => {
const companyRegister = ReactTestUtils.renderIntoDocument(
<CompanyRegisterModal />);
expect(companyRegister).to.exist;
});
it('handle password validation', () => {
const password = '3s24Aasd';
expect(validatePassword(password)).to.equal(true);
});
});
......@@ -66,6 +66,9 @@ export default class CompanyRegisterModal extends React.Component {
handlePassword = (e) => {
if (e.target.name === 'password') this.passwordField = e.target; else
if (e.target.name === 'password-confirm') this.passwordConfirmField = e.target;
const validatePassword = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}/g;
if(!validatePassword.test(this.passwordField.value)) this.passwordField.setCustomValidity("Must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters");
else this.passwordField.setCustomValidity("");
const isExist = this.passwordField && this.passwordConfirmField;
if (isExist) {
if (this.passwordField.value !== this.passwordConfirmField.value) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment