【发布时间】:2021-03-28 16:45:59
【问题描述】:
我目前正在尝试为我正在开发的一个网站创建一个登录/创建帐户页面。在创建帐户表单中,我有一个用于用户名、姓名、电子邮件、密码和验证密码的输入字段。我在输入字段中也有模式,以便用户制作有效的帐户信息。这是下面的表格:
<form method="post" action="CreateAccount.php">
<h1>Create Account</h1>
<input class="inputInfo" type="text" name="username" id="newUsername" pattern="[A-Za-z0-9]+" placeholder="Username" maxlength="40" minlength="5" onkeyup="checkInput()" onblur="checkInput()" autocomplete="off" required/>
<input class="inputInfo" type="text" name="fullname" id="newName" placeholder="First and Last Name" onkeyup="checkInput()" onblur="checkInput()" minlength="3" autocomplete="off" required/>
<input class="inputInfo" type="email" name="email" id="newEmail" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" title="Must be a real email" placeholder="Email" onkeyup="checkInput()" onblur="checkInput()" required/>
<input class="inputInfo" type="password" name="password" id="newPassword" pattern="(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$" placeholder="Password"
title="Must be 8 or more characters with at least one number or special character, uppercase letter, and lowercase letter" onkeypress="checkInput()" onblur="checkInput()" required/>
<input class="inputInfo" type="password" name="verifypassword" id="verifyPass" pattern="(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$" placeholder="Verify Password"
title="Must be 8 or more characters with at least one number or special character, uppercase letter, and lowercase letter" onkeypress="checkInput()" onblur="checkInput()" required/>
<span><label for="showp"><input type="checkbox" id="showp" onclick="showPassword()">Show Password</label></span>
<button type="submit" style="margin-top: 7px;" class="disabled" id="submitButton">Sign Up</button>
<p style="font-size: 10px;">By signing up, you agree to our Terms , Data Policy and Cookies Policy .</p>
</form>
为了澄清:用户名模式要求您的用户名只能包含大小写字母和数字,并且必须至少有 5 个字符,最多 40 个字符。电子邮件要求您输入有效的电子邮件地址模式。并且密码要求密码至少有8个字符,并且必须有大小写字母和数字或特殊字符。
在输入字段中,您将看到我有一个在模糊或 keyup 事件期间调用的函数,称为 checkInput()。该函数的目的是确保输入字段具有必要的长度,然后才能启用提交按钮:
function checkInput ()
{
let usernameLength = document.getElementById('newUsername').value.length;
let nameLength = document.getElementById('newName').value.length;
let emailLength = document.getElementById('newEmail').value.length;
let passwordLength = document.getElementById('newPassword').value.length;
let verifyLength = document.getElementById('verifyPass').value.length;
if (usernameLength >= 5 && nameLength > 0 && emailLength > 0 && passwordLength >= 8 && verifyLength >= 8)
{
document.getElementById('submitButton').disabled = false;
const element = document.querySelector('#submitButton');
if (element.classList.contains('disabled'))
{
element.classList.remove('disabled');
}
}
else
{
document.getElementById('submitButton').disabled = true;
const addElement = document.querySelector('#submitButton');
addElement.classList.add('disabled');
}
}
我还有以下 CSS 类,它们可以使输入字段的边框和阴影变为绿色或红色:
.validInput {
border-color: #50c878;
box-shadow: 0 0 5px #50c878;
}
.invalidInput {
border-color: #ff0000;
box-shadow: 0 0 5px #ff0000;
}
我的问题是我想添加一些 javascript,以便当用户在表单中输入他们的信息时,代码会检查以确保他们的输入与输入字段中所述的模式相匹配。如果他们放入该字段的输入是有效的,我希望 javascript 将 validInput 类添加到输入字段。如果输入无效,我想将 invalidInput 类添加到输入字段。不过,我不知道如何让 JavaScript 检查输入是否符合模式。
我还想在每次用户发生更改事件时检查输入是否有效。
有没有人对如何去做这件事有任何想法?
【问题讨论】:
-
您可以使用
document.getElementById('newUsername').addEventListener("change", checkInput)并为其他输入执行此操作。 -
@sidc
change事件仅在用户“提交”新值时触发,例如从输入中移除焦点。 OP 将希望改用input事件,该事件在每次击键时触发。
标签: javascript html forms