Java 脚本密码匹配和复杂性问题
Java script password match and complexity issues
这是我的脚本代码
function validateForm() {
var u = document.forms["myForm"]["uname"].value;
var x = document.forms["myForm"]["password1"].value;
var xx = document.forms["myForm"]["password2"].value;
if (u=="" || ( x != /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/ || (x != xx || x=="" ))) {
alert("Passwords do not match or username is blank. Passwords must be a minimum of 8 characters and contain at least 1 UPPER case 1 Lower case and 1 number");
return false;
}
}
这是完整的代码
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm() {
var u = document.forms["myForm"]["uname"].value;
var x = document.forms["myForm"]["password1"].value;
var xx = document.forms["myForm"]["password2"].value;
if (u=="" || ( x != (?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20} || (x != xx || x=="" ))) {
alert("Passwords do not match or username is blank. Passwords must be a minimum of 8 characters and contain at least 1 UPPER case 1 Lower case and 1 number");
return false;
}
}
</script>
</head>
<body>
<H1>Crypt for htpasswd file</H1><br>
<form name="myForm" action="htpasswd.php" onsubmit="return validateForm()" method="post">
Username:<br> <input TYPE="text" NAME="uname"><br>
Password:<br> <input TYPE="password" NAME="password1"><br>
Password:<br> <input TYPE="password" NAME="password2">
<input type="submit" value="Submit">
</form>
</body>
</html>
无论发生什么情况,它都会提示密码不正确。
与脚本中的正则表达式有关,我无法理解why.adsad
您无法使用简单的 equality/inequality 运算符测试正则表达式匹配。您可以为此使用 RegExp.test()
。
if (u === "" ||
!/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}/.test(x) ||
x !== xx) {
...
}
演示:http://jsfiddle.net/w0svjzd2/[已为 fiddle 演示故意禁用表单提交]
你的使用方式不对 regex:
function validateForm() {
var u = document.forms["myForm"]["uname"].value;
var x = document.forms["myForm"]["password1"].value;
var xx = document.forms["myForm"]["password2"].value;
// Create a pattern obj
var patt = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/;
if (u=="" || ( patt.test(x) || (x != xx || x=="" ))) {
alert("Passwords do not match or username is blank. Passwords must be a minimum of 8 characters and contain at least 1 UPPER case 1 Lower case and 1 number");
return false;
}
}
使用 test()
方法检查字符串是否与正则表达式匹配。
像这样-
> x="1aA123"
> /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/.test(x)
>> true
试试这个...
<script type="text/javascript">
function checkForm(form)
{
if(form.username.value == "") {
alert("Error: Username cannot be blank!");
form.username.focus();
return false;
}
re = /^\w+$/;
if(!re.test(form.username.value)) {
alert("Error: Username must contain only letters, numbers and underscores!");
form.username.focus();
return false;
}
if(form.pwd1.value != "" && form.pwd1.value == form.pwd2.value) {
if(form.pwd1.value.length < 6) {
alert("Error: Password must contain at least six characters!");
form.pwd1.focus();
return false;
}
if(form.pwd1.value == form.username.value) {
alert("Error: Password must be different from Username!");
form.pwd1.focus();
return false;
}
re = /[0-9]/;
if(!re.test(form.pwd1.value)) {
alert("Error: password must contain at least one number (0-9)!");
form.pwd1.focus();
return false;
}
re = /[a-z]/;
if(!re.test(form.pwd1.value)) {
alert("Error: password must contain at least one lowercase letter (a-z)!");
form.pwd1.focus();
return false;
}
re = /[A-Z]/;
if(!re.test(form.pwd1.value)) {
alert("Error: password must contain at least one uppercase letter (A-Z)!");
form.pwd1.focus();
return false;
}
} else {
alert("Error: Please check that you've entered and confirmed your password!");
form.pwd1.focus();
return false;
}
alert("You entered a valid password: " + form.pwd1.value);
return true;
}
</script>
<form ... onsubmit="return checkForm(this);">
<p>Username: <input type="text" name="username"></p>
<p>Password: <input type="password" name="pwd1"></p>
<p>Confirm Password: <input type="password" name="pwd2"></p>
<p><input type="submit"></p>
</form>
这是我的脚本代码
function validateForm() {
var u = document.forms["myForm"]["uname"].value;
var x = document.forms["myForm"]["password1"].value;
var xx = document.forms["myForm"]["password2"].value;
if (u=="" || ( x != /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/ || (x != xx || x=="" ))) {
alert("Passwords do not match or username is blank. Passwords must be a minimum of 8 characters and contain at least 1 UPPER case 1 Lower case and 1 number");
return false;
}
}
这是完整的代码
<!DOCTYPE html>
<html>
<head>
<script>
function validateForm() {
var u = document.forms["myForm"]["uname"].value;
var x = document.forms["myForm"]["password1"].value;
var xx = document.forms["myForm"]["password2"].value;
if (u=="" || ( x != (?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20} || (x != xx || x=="" ))) {
alert("Passwords do not match or username is blank. Passwords must be a minimum of 8 characters and contain at least 1 UPPER case 1 Lower case and 1 number");
return false;
}
}
</script>
</head>
<body>
<H1>Crypt for htpasswd file</H1><br>
<form name="myForm" action="htpasswd.php" onsubmit="return validateForm()" method="post">
Username:<br> <input TYPE="text" NAME="uname"><br>
Password:<br> <input TYPE="password" NAME="password1"><br>
Password:<br> <input TYPE="password" NAME="password2">
<input type="submit" value="Submit">
</form>
</body>
</html>
无论发生什么情况,它都会提示密码不正确。
与脚本中的正则表达式有关,我无法理解why.adsad
您无法使用简单的 equality/inequality 运算符测试正则表达式匹配。您可以为此使用 RegExp.test()
。
if (u === "" ||
!/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}/.test(x) ||
x !== xx) {
...
}
演示:http://jsfiddle.net/w0svjzd2/[已为 fiddle 演示故意禁用表单提交]
你的使用方式不对 regex:
function validateForm() {
var u = document.forms["myForm"]["uname"].value;
var x = document.forms["myForm"]["password1"].value;
var xx = document.forms["myForm"]["password2"].value;
// Create a pattern obj
var patt = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/;
if (u=="" || ( patt.test(x) || (x != xx || x=="" ))) {
alert("Passwords do not match or username is blank. Passwords must be a minimum of 8 characters and contain at least 1 UPPER case 1 Lower case and 1 number");
return false;
}
}
使用 test()
方法检查字符串是否与正则表达式匹配。
像这样-
> x="1aA123"
> /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/.test(x)
>> true
试试这个...
<script type="text/javascript">
function checkForm(form)
{
if(form.username.value == "") {
alert("Error: Username cannot be blank!");
form.username.focus();
return false;
}
re = /^\w+$/;
if(!re.test(form.username.value)) {
alert("Error: Username must contain only letters, numbers and underscores!");
form.username.focus();
return false;
}
if(form.pwd1.value != "" && form.pwd1.value == form.pwd2.value) {
if(form.pwd1.value.length < 6) {
alert("Error: Password must contain at least six characters!");
form.pwd1.focus();
return false;
}
if(form.pwd1.value == form.username.value) {
alert("Error: Password must be different from Username!");
form.pwd1.focus();
return false;
}
re = /[0-9]/;
if(!re.test(form.pwd1.value)) {
alert("Error: password must contain at least one number (0-9)!");
form.pwd1.focus();
return false;
}
re = /[a-z]/;
if(!re.test(form.pwd1.value)) {
alert("Error: password must contain at least one lowercase letter (a-z)!");
form.pwd1.focus();
return false;
}
re = /[A-Z]/;
if(!re.test(form.pwd1.value)) {
alert("Error: password must contain at least one uppercase letter (A-Z)!");
form.pwd1.focus();
return false;
}
} else {
alert("Error: Please check that you've entered and confirmed your password!");
form.pwd1.focus();
return false;
}
alert("You entered a valid password: " + form.pwd1.value);
return true;
}
</script>
<form ... onsubmit="return checkForm(this);">
<p>Username: <input type="text" name="username"></p>
<p>Password: <input type="password" name="pwd1"></p>
<p>Confirm Password: <input type="password" name="pwd2"></p>
<p><input type="submit"></p>
</form>