If you want to validate a field in js for example a password field, you can use the regex, is a sequence of characters that forms a search pattern
I have the pwdField
and my const to test is const validPwd = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])([A-Za-z\d$@$!%*?&]|[^ ]){8,}$/;
- Min 8 characters
- at least one uppercase or lowercase letter
- contains $@$!%*?&
You can use the test method to validate the field.
Example:
const getPwdAndValidate = (pwdField, pwdField2) => {
const validPwd = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])([A-Za-z\d$@$!%*?&]|[^ ]){8,}$/;
if (pwdField === pwdField2 && validPwd.test(pwdField) && pwdField.length >= 8) {
return pwdField;
} else {
return null;
}
};