Im creating accounts on a api and the password needs those restrictions.The handleRegist() is connecting to the api but the passwords i use always goes through that if cycle.I am using a function to check the password strength for a login the password must contain at least 1 special character 1 uppercase letter and it must be longer than 8 characters.
I have this code so far:
function passwordChecker(password){
var strongPassword = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$/;
if (password.value.match(strongPassword)) {
console.log("Good Password");
handleRegist();
}else
console.log("Try Again!\nnot strong enough");
}
NOTE: the strongPassword variable i found it on the internet and i am not sure if the restriction works like that.
>Solution :
To be able to fire the function you need to call it from somewhere, naturally, you would like to call it inside the onchange event inside the input component.
<input
type="text"
onChange={(e) => passwordChecker({ value: e.target.value })}
/>
Here is a working codesandbox example:
Working example
(open the console from the bottom of the page to see the result while typing inside the input)
Notice: im passing the value inside an object {value: e.target.value}, im doing it because inside your function you are checking password.value.match(strongPassword), it expect to get password.value, therefore i needed to add the value inside an object and pass it to the function.
i recommend to handle the value and store it in a state with useState and pass the state as value to the input.