写一个js函数检查页面中的登录用户名,密码是否为空,且密码长度不少于6
<form name="loginform">
<input type="text" name="user" id="user" />
<input type="password" name="pwd" id="pwd" />
</form>
<script>
function $(id){return document.getElementById(id);}
function trim(s){return s.replace(/(^\s*)|(\s*$)/g,"");}
function Login(){
var user=$("user").value;
var pwd=$("pwd").value;
if(!trim(user)){alert("用户名不能为空");$("user").focus();return false;}
if(!trim(pwd)){alert("密码不能为空");$("pwd").focus();return false;}
if(pwd.length<6){alert("密码不能少于6位数");$("pwd").focus();return false;}
if(pwd.length>16){alert("密码不能大于16位数");$("pwd").focus();return false;}
$("loginform").submit();
}
</script>
<form name="loginform" id="loginform">
<input type="text" name="user" id="user" />
<input type="password" name="pwd" id="pwd" />
<input type="button" value="提交" onclick="Login();" />
</form>
function $(id){return document.getElementById(id);}
function trim(s){return s.replace(/(^\s*)|(\s*$)/g,"");}
function Login(){
var user=$("user").value;
var pwd=$("pwd").value;
if(!trim(user)){alert("用户名不能为空");$("user").focus();return false;}
if(!trim(pwd)){alert("密码不能为空");$("pwd").focus();return false;}
if(pwd.length<6){alert("密码不能少于6位数");$("pwd").focus();return false;}
if(pwd.length>16){alert("密码不能大于16位数");$("pwd").focus();return false;}
$("loginform").submit();
}
</script>
<form name="loginform" id="loginform">
<input type="text" name="user" id="user" />
<input type="password" name="pwd" id="pwd" />
<input type="button" value="提交" onclick="Login();" />
</form>
温馨提示:答案为网友推荐,仅供参考
第1个回答 2015-02-28
var user = document.getElementById("user").valueOf();
var pwd = document.getElementById("pwd").valueOf();
if(pwd!=null&&pwd.length>5 && user!=null&&user.length>5){
alert("success");
}
你也可以将判断语句分开写
var pwd = document.getElementById("pwd").valueOf();
if(pwd!=null&&pwd.length>5 && user!=null&&user.length>5){
alert("success");
}
你也可以将判断语句分开写
第2个回答 推荐于2018-01-04
<form name="loginform">
<input type="text" name="user" id="user" />
<input type="password" name="pwd" id="pwd" />
<input type="submit" value="登陆" onclick="return judge();"/>
</form>
js函数:
function judge(){
var user = document.getElementById("user").value;
var pwd= document.getElementById("pwd").value;
if(user.trim() == "" || pwd.trim() == ""){
alert("用户和密码不能为空");
return false;
}else if(pwd.length < 6){
alert("密码不能低于6位");
return false;
}
return true;
}本回答被网友采纳
<input type="text" name="user" id="user" />
<input type="password" name="pwd" id="pwd" />
<input type="submit" value="登陆" onclick="return judge();"/>
</form>
js函数:
function judge(){
var user = document.getElementById("user").value;
var pwd= document.getElementById("pwd").value;
if(user.trim() == "" || pwd.trim() == ""){
alert("用户和密码不能为空");
return false;
}else if(pwd.length < 6){
alert("密码不能低于6位");
return false;
}
return true;
}本回答被网友采纳