<!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"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>Document</title> <style type="text/css"> .td_left{ width:150px; text-align:right; } </style> <script type="text/javascript"> function saveform(form){ var email=form.form_email.value; var password=form.form_password.value; var confirmpassword=form.form_checkpassword.value; if(email !="" && !checkemail(email)){ alert("邮箱格式错误,请重新填写"); return false; }else if(email==""){ alert("邮箱不能为空"); return false; } } var checkpasswordinfo=checkpassword(password,confirmpassword); if(checkpasswordinfo != "success"){ info.innerHTML=checkpasswordinfo; return false; } function checkemail(email){ var regu="^([\.A-Za-z0-9_-])+@([A-Za-z0-9_-])+(\.[A-Za-z0-9_-])+"; var re=new RegExp(regu); return re.test(email); } function checkpassword(password,confirmpassword){ var info="success"; if(password=="" || confirmpassword==""){ alert("密码/确认密码不能为空"); } else{ if(password != confirmpassword){ alert("密码/确认密码请保持一致"); } } } </script> </head> <body> <div> <form method="post"> <table> <tr> <td>邮箱:</td> <td><input type="email" name="form_email"/></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="form_password"/></td> </tr> <tr> <td>确认密码:</td> <td><input type="password" name="form_checkpassword"/></td> </tr> <tr> <td></td> <td><input type="button" onclick="saveform(this)" value="登录"/></td> </tr> </table> </form> </div> </body></html>
1 回答
李晓健
TA贡献1036条经验 获得超461个赞
<tr> <td></td> <!--这里不能传this 这里的this指向的是按钮,不是form--> <td><input type="button" onclick="saveform()" value="登录"/></td> </tr>
function saveform(){ //这里的form 需要通过id获取 没法传进来 var form = document.getElementById('form'); var email=form.form_email.value; var password=form.form_password.value; var confirmpassword=form.form_checkpassword.value; if(email !="" && !checkemail(email)){ alert("邮箱格式错误,请重新填写"); return false; }else if(email==""){ alert("邮箱不能为空"); return false; } /** * 密码验证这一块是不是也应该写在saveform里面 */ var checkpasswordinfo=checkpassword(password,confirmpassword); if(checkpasswordinfo != "success"){ //这个info 没有定义 // info.innerHTML=checkpasswordinfo; return false; } } function checkemail(email){ var regu="^([\.A-Za-z0-9_-])+@([A-Za-z0-9_-])+(\.[A-Za-z0-9_-])+"; var re=new RegExp(regu); return re.test(email); } function checkpassword(password,confirmpassword){ var info="success"; if(password=="" || confirmpassword==""){ alert("密码/确认密码不能为空"); //这里需要更新info info = 'error'; } else{ if(password != confirmpassword){ alert("密码/确认密码请保持一致"); //这里需要更新info info = 'error'; } } // 这里需要返回info return info; }
添加回答
举报
0/150
提交
取消