gpt4 book ai didi

JavaScript 和 HTML 正则表达式不工作

转载 作者:行者123 更新时间:2023-11-27 23:10:43 24 4
gpt4 key购买 nike

我有一个 php 页面:

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
echo $_POST["username"];
echo $_POST["password"];
echo $_POST["confirm_password"];
echo $_POST["email"];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>WordCat Signups</title>
<script type="text/javascript" src="signup_validate.js"></script>
</head>
<body>
<form action="" method="POST">
Username:<br><input type="text" name="username" placeholder="Username" onkeyup="check_username();"><span id="check_username"></span><br>
Password:<br><input type="password" name="password" placeholder="Password" onkeyup="check_password();"><span id="check_password"></span><br>
Confirm Password:<br><input type="password" name="confirm_password" placeholder="Confirm Password" onkeyup="check_passwords();"><span id="check_passwords"></span><br>
Email:<br><input type="email" name="email" placeholder="Email" onkeyup="check_email();"><span id="check_email"></span><br>
<input type="submit" name="submit" value="Signup"></br>
</form>
</body>
</html>

我有一些用于检查字段的 JavaScript:

function check_username(){
var username = document.getElementsByName("username")[0].value;
var pattern = /^[\w[^!\"\#$%&\'()*+,\-.\/:;<=>?@\[\\\]^_`{|}~]]{1,32}$/;
if(pattern.test(username)){
document.getElementById("check_username").innerHTML = "Username is valid.";
} else {
document.getElementById("check_username").innerHTML = "Username is invalid. It should only contain letters, numbers, and underscores, as well as have a maximum of 32 characters.";
}
}
function check_password(){
var password = document.getElementsByName("password")[0].value;
var pattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)([a-zA-z0-9!\"\#$%&\'()*+,\-.\/:;<=>?@\[\\\]^_`{|}~]{6,})$/;
if(pattern.test(password)){
document.getElementById("check_password").innerHTML = "Password is valid.";
} else {
document.getElementById("check_password").innerHTML = "Password is invalid. It should have at least 6 characters, and at least one number, uppercase, and lowercase character.";
}
}
function check_passwords(){
var password = document.getElementsByName("password")[0].value;
var confirm_password = document.getElementsByName("confirm_password")[0].value;
if(password == confirm_password){
document.getElementById("check_passwords").innerHTML = "Passwords match";
} else {
document.getElementById("check_passwords").innerHTML = "Passwords do not match.";
}
}

但是,对于 JavaScript,用户名和确认密码测试有效,但密码正则表达式无效。当我输入有效密码时。正则表达式可以在这里找到 link它对所有测试都是正确的。但是,当使用我的浏览器时,正则表达式无法在线获取有效密码!

最佳答案

我看不出有什么理由在像你这样的复杂情况下使用单个正则表达式(除了炫耀你的正则表达式)......

if(
password.length >= 6 && // at least 6 characters
/[0-9]/.test(password) && // at least one number
/[a-z]/.test(password) && // at least one lowercase character
/[A-Z]/.test(password) // at least one lowercase character
) {
// yay :)
}

关于JavaScript 和 HTML 正则表达式不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46176247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com