【发布时间】:2018-02-10 20:52:01
【问题描述】:
我是一名初级开发人员,一直在尝试使用 javascript 编写验证表单。验证弹出窗口似乎没有出现。有人可以看看我的代码并告诉我我缺少什么吗?
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="udacity-grader" content="http://udacity.github.io/course-web-forms/lesson2/quizCustomValidity/grader/tests.json" libraries="jsgrader" unit-tests="http://udacity.github.io/course-web-forms/lesson2/quizCustomValidity/grader/unit_tests.js">
<title>Quiz - setCustomValidity</title>
</head>
<body>
<h3>Create a new password</h3>
<p>Password should meet the following requirements:</p>
<ul>
<li>16-100 characters (longer is better)</li>
<li>At least one of these symbols: !, @, #, $, %, ^, &, *</li>
<li>At least one number</li>
<li>At least one lowercase letter</li>
<li>At least one uppercase letter</li>
</ul>
<label>
<input id="first" type="password" placeholder="New password" min="16" max="100" autofocus>
</label>
<label>
<input id="second" type="password" min="16" max="100"placeholder="Repeat password">
</label>
<input id="submit" type="submit">
<script src="app.js"></script>
</body>
</html>
var firstPasswordInput = document.querySelector('#first');
var secondPasswordInput = document.querySelector('#second');
var submit = document.querySelector('#submit');
submit.onclick = function() {
if(firstPasswordInput != secondPasswordInput){
secondPasswordInput.setCustomValidity('password does not match');
}
if (firstPasswordInput.includes('/[\!\@\#\$\%\^\&\*]/g')== false)
{ firstPasswordInput.setCustomValidity('password must include one of the following !@#$%^&*');
}
if (firstPasswordInput.includes('/[0-9]/g')== false)
{ firstPasswordInput.setCustomValidity('password must include a number');
}
if (firstPasswordInput.includes('/[a-z]/g')== false)
{ firstPasswordInput.setCustomValidity('password must include a lowercase letter');
}
if (firstPasswordInput.includes('/[A-Z]/g')== false)
{ firstPasswordInput.setCustomValidity('password must include an uppercase letter');
}
};
【问题讨论】:
-
为什么将密码限制为最多 100 个字符?
-
嘿,我想你忘了把你的javascript放在
<script>和</script>标签中;)
标签: javascript html forms