英文:
Does anyone know why this code is allowing passwords to go through that it shouldn't?
问题
以下是您的JavaScript代码的翻译部分:
const firstPass = document.querySelector("#firstPass");
const secondPass = document.querySelector("#secondPass");
const errorText = document.querySelector("#error-text");
const myButton = document.querySelector("#button");
let passwordStrength = false;
/**
Regex表达式确保密码至少包含6个字符,其中至少包括一个大写字母、一个小写字母和一个特殊字符。
*/
let regex = new RegExp('^(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*(),.?":{}|<>])(?=.*[0-9]).{6,}$');
// 检查密码是否符合要求
if (regex.test(firstPass.value)) {
passwordStrength = true;
}
// 当提交表单的按钮被点击时,调用此函数。
// 如果密码不匹配,它会显示错误消息并阻止提交。
// 否则,它会简要描述成功消息并允许继续提交。
myButton.onclick = function() {
if (firstPass.value != secondPass.value) {
errorText.style.display = "block";
errorText.classList.remove("matched");
errorText.textContent = "错误!密码不匹配!";
return false;
} else if (passwordStrength === false) {
errorText.style.display = "block";
errorText.classList.remove("matched");
errorText.textContent = "密码必须至少包含6个字符,包括一个特殊字符,以及一个小写字母和一个大写字母。";
return false;
} else {
errorText.style.display = "block";
errorText.classList.add("matched");
errorText.classList.remove("text-danger");
errorText.textContent = "密码匹配。";
return true;
}
};
请注意,这只是您代码的JavaScript部分的翻译。如果您需要HTML部分的翻译,请告诉我。
英文:
I have this snippet of Javascript code that is meant to ensure that two passwords are the same before the user submits, as well as ensure that the password strength is up to a certain standard. It correctly blocks passwords that are not the same, but it does not block passwords that are too short or do not fulfill the regex requirements.
Here is my code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const firstPass = document.querySelector("#firstPass");
const secondPass = document.querySelector("#secondPass");
const errorText = document.querySelector("#error-text");
const myButton = document.querySelector("#button");
let passwordStrength = false;
/**
Regex expression makes sure it has at least 6 characters of which at least one is an uppercase letter,
one is a lowercase letter, and one is a special character.
*/
let regex = new RegExp('^(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*(),.?":{}|<>])(?=.*[0-9]).{6,}$');
// Checks to make sure password follows requirements
if (regex.test(firstPass.value)) {
passwordStrength = true;
}
// When the button to submit the form is submitted it calls this function.
// If the passwords do not match, it throws up an error message and doesn't allow the submission to go through.
// Otherwise it briefly describes a success message and allows the submission to continue.
myButton.onclick = function() {
if (firstPass.value != secondPass.value) {
errorText.style.display = "block";
errorText.classList.remove("matched");
errorText.textContent = "Error! The passwords do not match!";
return false;
} else if (passwordStrength === false) {
errorText.style.display = "block";
errorText.classList.remove("matched");
errorText.textContent = "The password must be at least 6 characters long, have a special character, as well as have a lower and uppercase letter.";
return false;
} else {
errorText.style.display = "block";
errorText.classList.add("matched");
errorText.classList.remove("text-danger");
errorText.textContent = "Passwords match.";
return true;
}
};
<!-- language: lang-html -->
<form action="protected/register.inc.php" method="post" autocomplete="off">
<p class="text-center">New Profile:</p>
<!-- Name input -->
<div class="form-outline mb-4">
<input type="text" id="registerName" class="form-control" name="registerName" />
<label class="form-label" for="registerName">First Name</label>
</div>
<!-- Username input -->
<div class="form-outline mb-4">
<input type="text" id="registerLastName" class="form-control" name="registerLastName" />
<label class="form-label" for="registerLastName">Last Name</label>
</div>
<!-- Email input -->
<div class="form-outline mb-4">
<input type="email" id="registerEmail" class="form-control" name="registerEmail" />
<label class="form-label" for="registerEmail">Email</label>
</div>
<!-- Password input -->
<div class="form-outline mb-4">
<input type="password" id="firstPass" class="form-control" name="firstPass" />
<label class="form-label" for="firstPass">New Password</label>
</div>
<div id="error-text" class="text-danger"></div>
<!-- Confirm password input -->
<div class="form-outline mb-4">
<input type="password" id="secondPass" class="form-control" name="secondPass" />
<label class="form-label" for="secondPass">Repeat Password</label>
</div>
<!-- Submit button -->
<button id="button" type="submit" class="btn btn-primary btn-block mb-3">Register</button>
</form>
<!-- end snippet -->
答案1
得分: 1
我不确定这是否是问题的一部分,因为它似乎可能会导致相反的问题,但您仅在页面加载时检查正则表达式,而不是在按钮点击时检查。您不是需要在每次单击提交按钮时检查正则表达式并更新密码强度吗?
如果我将正则表达式测试放在myButton.onclick函数内部,您的示例对我有效。
英文:
I'm not sure if this is part of the issue, since it seems like it might cause the opposite problem, but you are only checking the regex on page load and not on button click. Wouldn't you need to check the regex and update passwordStrength each time the submit button is clicked?
Your example is working for me if I put the regex test inside the myButton.onclick function.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论