简单的表单提交格式校验,总是不执行,跳过了,不知道错在哪?
论文问答
1
<%-- <八大内置对象之一,获取当前路径--%>
<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>
-
<!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>
-
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>
发表回复