<script language="javascript">
function check(){
if(document.getElementById('username').value==""){
alert("用戶名不能為空");
return false;
}if(....){
//判斷密碼;(省略了)
}
return ture;}
</script>
为什么当我输入用户名后,就不再判断密码了呢?
我想在这一个函数function check()中判断用户名和密码,
要么哪位大哥,帮我写一个完整的代码了,
function check(){
if(document.getElementById('username').value==""){
alert("用戶名不能為空");
return false;
}if(....){
//判斷密碼;(省略了)
}
return ture;}
</script>
为什么当我输入用户名后,就不再判断密码了呢?
我想在这一个函数function check()中判断用户名和密码,
要么哪位大哥,帮我写一个完整的代码了,
if(document.getElementById("department1").value== ''){
alert('Please select department!');
return false;
}
if(document.getElementById("department2").value== ''){
alert('Please select department!');
return false;
}
var invalid = " "; // Invalid character is a space
var minLength = 6; // Minimum length
var pw1 = document.getElementById("pw").value;
var pw2 = document.getElementById("cp").value;
// check for a value in both fields.
if (pw1 == '' || pw2 == '') {
alert('Please enter your password twice.');
return false;
}
// check for minimum length
if (document.getElementById("pw").value.length < minLength) {
alert('Your password must be at least ' + minLength + ' characters long. Try again.');
return false;
}
// check for spaces
if (document.getElementById("pw").value.indexOf(invalid) > -1) {
alert("Sorry, spaces are not allowed.");
return false;
}
else {
if (pw1 != pw2) {
alert ("You did not enter the same new password twice. Please re-enter your password.");
return false;
}
else {
alert('Please nice job !');
return true;
}
}}
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>两次密码输入验证</title>
<script language="javascript">
function validatePwd() { //验证两次输入的密码是否一致,并且至少6位
// if(document.getElementById("department1").value== ''){
// alert('Please select department!');
// return false;
// }
// if(document.getElementById("department2").value== ''){
// alert('Please select department!');
// return false;
// }
var invalid = " "; // Invalid character is a space
var minLength = 6; // Minimum length
var pw1 = document.getElementById("pw").value;
var pw2 = document.getElementById("cp").value;
// check for a value in both fields.
if (pw1 == '' || pw2 == '') {
alert('Please enter your password twice.');
return false;
}
// check for minimum length
if (document.getElementById("pw").value.length < minLength) {
alert('Your password must be at least ' + minLength + ' characters long. Try again.');
return false;
}
// check for spaces
if (document.getElementById("pw").value.indexOf(invalid) > -1) {
alert("Sorry, spaces are not allowed.");
return false;
}
else {
if (pw1 != pw2) {
alert ("You did not enter the same new password twice. Please re-enter your password.");
return false;
}
else {
alert('Please nice job !');
return true;
}
}}
</script>
<style>
body{
text-align:center;
}
#container{
width:450px;
height:300px;
border:1px solid green;
text-align:center;
}
</style></head><body>
<div id="container">
<form>
<input type="text" id="pw"/>
<input type="text" id="cp"/>
<input type="button" onclick="validatePwd()" value="校验"/>
</form>
</div></body>
</html>
放一起就有问题了, 放一起时,就是会判断 前面两个if() 了,后面的密码就不会判断了(也就是不输密码也不会提示了), 我发贴 的主要目的也就是要解决这个问题. 谢谢了!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script>
function checks()
{
if(document.getElementById("username").value=="")
{
alert("user error");
return false;
}
if(document.getElementById("password").value=="")
{
alert("pwd error");
return false;
}
return true;
}
</script>
</HEAD> <BODY>
<input type="text" name="username" />
<br/>
<input type="password" name="password" />
<br/>
<input type="button" onclick="return checks();" name="on_submit" value="測試下載" />
</BODY>
</HTML>