<%-- <八大内置对象之一,获取当前路径--%>
<form name="f" action="${pageContext.request.contextPath}/user/login.do" onsubmit="return check()" method="post" >
用户账号:<input type="text" name="id" placeholder="请输入账号....." ><br><br><br>
用户密码:<input type="text" name="password" placeholder="请输入密码....."><br><br><br>
<input type="submit" value="登录" >
<a href="${pageContext.request.contextPath}/user/to_register.do">
<input type=button value="注册" >
</a>
<a href="${pageContext.request.contextPath}/user/to_register.do">
<input type=button value="修改密码" >
</a>
</form>
<script type="text/javascript">
function check(){
var inputElement1 = document.getElementsByName("id");
var inputElement2 = document.getElementsByName("password");
if(/^\s*$/.test(inputElement1.value)){
alert("请输入用户名");
return false;
}
if( /^\s*$/.test(inputElement2.value)){
alert("请输入密码");
return false;
}
return true;
}
</script>
document.getElementsByName返回的是NodeList数组,不是DOM对象,要加[0]转为dom对象。要么用document.getElementById,但是需要给对应的input对象增加id="id",id="password"属性
有帮助麻烦点个采纳【本回答右上角】,谢谢~~有其他问题可以继续交流~
<%-- <八大内置对象之一,获取当前路径--%>
<form name="f" action="${pageContext.request.contextPath}/user/login.do" onsubmit="return check()" method="post">
用户账号:<input type="text" name="id" placeholder="请输入账号....."><br><br><br>
用户密码:<input type="text" name="password" placeholder="请输入密码....."><br><br><br>
<input type="submit" value="登录">
<a href="${pageContext.request.contextPath}/user/to_register.do">
<input type=button value="注册">
</a>
<a href="${pageContext.request.contextPath}/user/to_register.do">
<input type=button value="修改密码">
</a>
</form>
<script type="text/javascript">
function check(){
var inputElement1 = document.getElementsByName("id")[0];///转dom对象
var inputElement2 = document.getElementsByName("password")[0];///转dom对象
if (/^\s*$/.test(inputElement1.value)) {
alert("请输入用户名");
inputElement1.focus();///设置焦点
return false;
}
if( /^\s*$/.test(inputElement2.value)){
alert("请输入密码");
inputElement2.focus();///设置焦点
return false;
}
return true;
}
</script>
onsubmit="return check();return false"
如果有帮助希望能够采纳支持,谢谢。
问题其实比较简单,在使用正则表达式的时候判断有点问题,应该是取相反的结果才对。
改为:
结果:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form name="f" action="${pageContext.request.contextPath}/user/login.do" onsubmit="return check()" method="post">
用户账号:<input type="text" name="id" placeholder="请输入账号....."><br><br><br> 用户密码:
<input type="text" name="password" placeholder="请输入密码....."><br><br><br>
<input type="submit" value="登录">
<a href="${pageContext.request.contextPath}/user/to_register.do">
<input type=button value="注册">
</a>
<a href="${pageContext.request.contextPath}/user/to_register.do">
<input type=button value="修改密码">
</a>
</form>
<script type="text/javascript">
function check() {
var inputElement1 = document.getElementsByName("id");
var inputElement2 = document.getElementsByName("password");
if (!/^\s*$/.test(inputElement1.value)) {
alert("请输入用户名");
return false;
}
if (!/^\s*$/.test(inputElement2.value)) {
alert("请输入密码");
return false;
}
return true;
}
</script>
</body>
</html>