jsp中判断用户名等是否为空,框是否选中,怎么让提示错误后不跳转,停留在当前页,这段代码怎么改啊
<scripttype="text/javascript">functioncheckRegister(){varname=document.getElementById...
<script type="text/javascript">
function checkRegister(){
var name=document.getElementById("name");
if(name.value=="")
{
alert("请输入用户名!");
// location.href="user_register.jsp";
name.focus();
return;
}
var password=document.getElementById("password");
if(password.value=="")
{
alert("请输入密码!");
// location.href="user_register.jsp";
password.focus();
return;
}
var password2=document.getElementById("password2");
if(password.value!=password2.value)
{
alert("输入的密码不一致!");
password2.focus();
return;
}
var email=document.getElementById("email");
var emailValue=email.value;
var pattern=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/i;
var r=emailValue.search(pattern);
if(r==-1){
alert("email格式不正确,请重新输入!");
email.focus();
email.value="";
return;
}
var chk=document.getElementsById("agreement");
for(var i=0,len=chk.length;i<len;i++){
if(chk[i].type=="checkbox"){
if(!chk[i].checked){
alert("请先阅读协议!");
chk[i].focus();
return;
}
}
}
var register_form=document.getElementById("userRegister");
register_form.submit();
} 展开
function checkRegister(){
var name=document.getElementById("name");
if(name.value=="")
{
alert("请输入用户名!");
// location.href="user_register.jsp";
name.focus();
return;
}
var password=document.getElementById("password");
if(password.value=="")
{
alert("请输入密码!");
// location.href="user_register.jsp";
password.focus();
return;
}
var password2=document.getElementById("password2");
if(password.value!=password2.value)
{
alert("输入的密码不一致!");
password2.focus();
return;
}
var email=document.getElementById("email");
var emailValue=email.value;
var pattern=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/i;
var r=emailValue.search(pattern);
if(r==-1){
alert("email格式不正确,请重新输入!");
email.focus();
email.value="";
return;
}
var chk=document.getElementsById("agreement");
for(var i=0,len=chk.length;i<len;i++){
if(chk[i].type=="checkbox"){
if(!chk[i].checked){
alert("请先阅读协议!");
chk[i].focus();
return;
}
}
}
var register_form=document.getElementById("userRegister");
register_form.submit();
} 展开
展开全部
这样吧,我有个建议,你的jsp页面肯定有个表单吧,在<form>标签中添加onSubmit事件监听
如下:(注明:action跳转的表单是我自己随便写的)
<form name="userRegister" action="01.html" method="get" onSubmit="return checkRegister()">
然后加入你的js函数,不过要改改的
<script type="text/javascript">
function checkRegister(){
var name=document.getElementById("name");
if(name.value=="")
{
alert("请输入用户名!");
// location.href="user_register.jsp";
name.focus();
return false;
}
var password=document.getElementById("password");
if(password.value=="")
{
alert("请输入密码!");
// location.href="user_register.jsp";
password.focus();
return false;
}
var password2=document.getElementById("password2");
if(password.value!=password2.value)
{
alert("输入的密码不一致!");
password2.focus();
return false;
}
var email=document.getElementById("email");
var emailValue=email.value;
var pattern=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/i;
var r=emailValue.search(pattern);
if(r==-1){
alert("email格式不正确,请重新输入!");
email.focus();
email.value="";
return false;
}
var chk=document.getElementsById("agreement");
for(var i=0,len=chk.length;i<len;i++){
if(chk[i].type=="checkbox"){
if(!chk[i].checked){
alert("请先阅读协议!");
chk[i].focus();
return false;
}
}
}
return true;
}
你再试试吧!
展开全部
var a=0,b=0,c=0,d=;
return所在的if语句后面加
if(name.value==""){
/***/
}else{a=1}
if(password.value!=password2.value){
/***/
}else{b=1}
if(r==-1){
/***/
}else{c=1}
if(!chk[i].checked){
/***/
}else{d=1}
if(a==1 &&b==1&&c==1&&d==1){
var register_form=document.getElementById("userRegister");
register_form.submit();
}
return所在的if语句后面加
if(name.value==""){
/***/
}else{a=1}
if(password.value!=password2.value){
/***/
}else{b=1}
if(r==-1){
/***/
}else{c=1}
if(!chk[i].checked){
/***/
}else{d=1}
if(a==1 &&b==1&&c==1&&d==1){
var register_form=document.getElementById("userRegister");
register_form.submit();
}
追问
还是不行,在提示错误后,表单还是提交运行后面的
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询