验证信息不起作用,代码有错吗大神们帮我看看~~

LIUloveLI2011 2014-09-19 11:09:38
css部分
<form action="" method="post" name="myform" onsubmit="check();">
<tr>
<td>你的Email:</td>
<td><input id="email" type="text" name="email" class="input-text" /> </td>
</tr>
<tr>
<td>输入密码:</td>
<td><input id="password" type="password" name="password" class="input-text" /> </td>
</tr>
<tr>
<td>确认密码:</td>
<td><input id="repassword" type="password" name="password" class="input-text" /> </td>
</tr>
<tr>
<td>用户名:</td>
<td><input id="user" type="text" name="name" class="input-text" /> </td>
</tr>
</form>
js部分
function check(){
var email=document.getElementById("email").value;
if(email==""){
alert("email不能为空");
return false;
}
if(email.indexOf("@")==-1||email.indexOf(".")==-1){
alert("email格式不正确");
return false;
}
var pwd=document.getElementById("password").value;
if(pwd==""){
alert("密码不能为空");
return false;
}
if(pwd.length<6){
alert("密码必须大于6位");
return false;
}
var repwd=document.getElementById("repassword" );
if(repwd!=pwd){
alert("密码不一致");
return false;
}
var user=document.getElementById("user").value;
if(user==""){
alert("姓名不能为空");
return false;
}
for(var i=0;i<user.length;i++){
var j=user.substring(i,i+1);
if(isNaN(j)==false){
alert("姓名中不能包含数字");
return false;
}
}
return true;
}
...全文
253 12 打赏 收藏 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tony__1983 2014-09-19
  • 打赏
  • 举报
回复
按照LZ的代码 错误信息会报出来,但是在错误的情况下画面依然会跳转。 2楼的方法 是可以修正画面依然跳转的错误。
蝶恋花雨 2014-09-19
  • 打赏
  • 举报
回复
<form action="" method="post" name="myform" onsubmit="check()"> 改成下面的 <form action="xxx.aspx" method="post" name="myform" onsubmit="return check();"> 或者 <form action="" method="post" name="myform" onclick="return check();" > http://bbsanwei.iteye.com/blog/271547 http://liuzhen.liujie.blog.163.com/blog/static/1275561562012729104825183/
蝶恋花雨 2014-09-19
  • 打赏
  • 举报
回复
你是点什么直接执行表单提交的? 我觉得加个button 来提交 <input type="button" onclick="check();" value="提交" />这样没问题。
bichir 2014-09-19
  • 打赏
  • 举报
回复
注意写上return <form action="" method="post" name="myform" onsubmit="return check()">
tongki_88 2014-09-19
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
  <title>test</title>  
<style type="text/css">

</style>
<script type="text/javascript">
function check(){
 var email=document.getElementById("email").value;
 if(email==""){
alert("email不能为空");
return false; 
 }
 if(email.indexOf("@")==-1||email.indexOf(".")==-1){
 alert("email格式不正确");
 return false;
 }
  var pwd=document.getElementById("password").value;
  if(pwd==""){
  alert("密码不能为空");
  return false;
  }
   if(pwd.length<6){
   alert("密码必须大于6位");
   return false;
   }
 var repwd=document.getElementById("repassword" );
 if(repwd!=pwd){
 alert("密码不一致");
 return false;
 }
var user=document.getElementById("user").value;
if(user==""){
alert("姓名不能为空");
return false;
}
for(var i=0;i<user.length;i++){
var j=user.substring(i,i+1);
if(isNaN(j)==false){
alert("姓名中不能包含数字");
return false;
}
}
return true;
}
</script>
</head>
<body>
<form action="" method="post" name="myform" onsubmit="check();">
<table>
    <tr>   
       <td>你的Email:</td>
       <td><input id="email"  type="text" name="email" class="input-text" /> </td>   
    </tr>
    <tr>
       <td>输入密码:</td>
       <td><input id="password" type="password" name="password" class="input-text" /> </td>
    </tr>
    <tr>
       <td>确认密码:</td>
       <td><input  id="repassword" type="password" name="password" class="input-text" /> </td>
    </tr>
    <tr>
       <td>用户名:</td>
       <td><input  id="user" type="text" name="name" class="input-text" /> </td>
    </tr>
</table>
<input type="submit" value="submit" />
    </form>
</body>
</html>
save4me 2014-09-19
  • 打赏
  • 举报
回复
点击查看演示代码,已经测试过了,把你的js放在放在head里面
引用 9 楼 LIUloveLI2011 的回复:
[quote=引用 7 楼 kongwei521 的回复:] [quote=引用 6 楼 LIUloveLI2011 的回复:] 不行~~
<form action="" method="post" name="myform" onclick="return check();" > try [/quote] 不行~~[/quote]
小白看世界OL 2014-09-19
  • 打赏
  • 举报
回复
楼主 1L 和2L都不行的话 提示什么 你说一下 不然真吧帮不了你
LIUloveLI2011 2014-09-19
  • 打赏
  • 举报
回复
引用 7 楼 kongwei521 的回复:
[quote=引用 6 楼 LIUloveLI2011 的回复:] 不行~~
<form action="" method="post" name="myform" onclick="return check();" > try [/quote] 不行~~
别闹腰不好 2014-09-19
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>第一个简单的jQuery程序</title> <script language="JavaScript" type="text/javascript"> function check(){ var email=document.getElementById("email").value; if(email==""){ alert("email不能为空"); return false; } if(email.indexOf("@")==-1||email.indexOf(".")==-1){ alert("email格式不正确"); return false; } var pwd=document.getElementById("password").value; if(pwd==""){ alert("密码不能为空"); return false; } if(pwd.length<6){ alert("密码必须大于6位"); return false; } var repwd=document.getElementById("repassword" ).value;//这里没有value if(repwd!=pwd){ alert("密码不一致"); return false; } var user=document.getElementById("user").value; if(user==""){ alert("姓名不能为空"); return false; } for(var i=0;i<user.length;i++){ var j=user.substring(i,i+1); if(isNaN(j)==false){ alert("姓名中不能包含数字"); return false; } } return true; } </script> </head> <body> <form action="" method="post" name="myform" onsubmit="return check();"> <tr> <td>你的Email:</td> <td><input id="email" type="text" name="email" class="input-text" /> </td> </tr> <tr> <td>输入密码:</td> <td><input id="password" type="password" name="password" class="input-text" /> </td> </tr> <tr> <td>确认密码:</td> <td><input id="repassword" type="password" name="repassword" class="input-text" /> </td> <!-- 这里名字写错了 --> </tr> <tr> <td>用户名:</td> <td><input id="user" type="text" name="name" class="input-text" /> </td> </tr> <input type="submit" value="tijiao"/> </form> </body> </html>
蝶恋花雨 2014-09-19
  • 打赏
  • 举报
回复
引用 6 楼 LIUloveLI2011 的回复:
不行~~
<form action="" method="post" name="myform" onclick="return check();" > try
LIUloveLI2011 2014-09-19
  • 打赏
  • 举报
回复
引用 2 楼 kongwei521 的回复:
你是点什么直接执行表单提交的? 我觉得加个button 来提交 <input type="button" onclick="check();" value="提交" />这样没问题。
不行~~
LIUloveLI2011 2014-09-19
  • 打赏
  • 举报
回复
引用 1 楼 bichir 的回复:
注意写上return <form action="" method="post" name="myform" onsubmit="return check()">
还是不行……

87,984

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧