I'm trying to build a Password validator. My issue is that the boolean valid is always false even when the regex is tested to be correct. I am actually using typescript in my app, but I've made a demo here just in JSX:
The App.js file looks like this:
import React, { useState } from "react";
import "./styles.css";
const App = () => {
const [inputValue, setInputValue] = useState('');
const [valid, setValid] = useState(false);
const isValidPasswordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[#$@!%&*?])[A-Za-z\d#$@!%&*?]{12,}$/;
//const isValidPasswordRegex = /^\d+$/;
const validatePassword = (value) => {
if (!isValidPasswordRegex.test(value)) {
setValid(!valid);
} else {
setValid(valid);
}
return valid;
};
function onChange(e) {
setInputValue(e.target.value);
setValid(validatePassword(e.target.value));
}
return(
<div>
<div>Fill the password input and click elsewhere to blur the field</div>
<input
className={`${valid ? 'success' : 'error'}`}
onChange={onChange}
onBlur={onBlur}
value={inputValue}
/>
<div>{valid.toString()}</div>
</div>
);
}
export default App;
I've looked ay many similar questions on SO that seem to be using a very similar logic, but I assume that the app is re-rendering and valid is being set to false on each render. I thought useState was the correct implementation and binding this to an onChange event would test the input value each time it is changed. I was concerned my regex was not correct so I also tested a simpler condition (which is commented out here) but that is not the issue.