日期:2014-05-20  浏览次数:20548 次

新手分享一个JS问题
JScript code

function registe(){
    if(checkusername()&&checkpassword()&&checksex()&&checkage()){
        document.reg.action="showinfo.jsp";
        document.reg.submit();
    }else 
    if(checkusername()==false){
        alert("用户名输入有误");
        document.reg.reset();
    }else
    if(checkpassword()==false){
        alert("密码输入有误");
        document.reg.reset();
    }else
    if(checksex()==false){
        alert("性别必须选择");
        document.reg.reset();
    }else
    if(checkage()==false){
        alert("年龄输入有误");
        document.reg.reset();
    }else
    if(checkaddress()==false){
        alert("地址是必须选择地");
        document.reg.reset();
    }
}
function checkusername(){
     var username=document.getElementById("username").value;
     var reg=/^[a-zA-Z][a-zA-Z0-9_]{6,10}$/;
     var result=reg.test(username);
     if(result){
         return true;
     }
     return false;
}
function checkpassword(){
    var password=document.getElementById("password").value;
    if(password.length>6&&password.length<10){
        return true;
    }
    return false;
}
function checkpasswordequals(){
    
    var password=document.getElementById("password").value;
    var repassword=document.getElementById("repassword").value;
    if(password==repassword){
        return true;
    }else{
        alert("两次输入密码不一致");
        document.getElementById("password").value="";
        document.getElementById("repassword").value="";
        document.getElementById("password").focus();
    }
}
function checksex(){
    var sex=document.getElementsByName("sex");
    var sex1=document.getElementsByName("sex").value;
    alert(sex1);
    var num=0;
    for(var i=0;i<sex.length;i++){
        if(sex[i].checked){
            num=num+1;
        }
    } 
    if(num==0){
        return false;
    }else{
        return true;
    }
}
function checkage(){
    var age=document.getElementById("age").value ;
    if(parseInt(age)>0&&parseInt(age)<150){
        return true;
    }
    return false;
}
function checkaddress(){
    var add1=document.getElementById("province").value;
    var add2=document.getElementById("city").value;
    if((add1==0)||(add2==1)){
        return false;
    }
    return true;
}




如果使用多个IF 而没有使用else的话,JS会先将全部的if()的内容全部执行完才会进入到方法体中去!但是在java中不会出现这个问题!所以在编写JS时注意下把!不要省略else

希望能帮上大家!

------解决方案--------------------
你的理解有点问题。
document.reg.submit();就是开了一个线程。然后原来的javascript方法仍然可以继续执行。两边是并行的。你可以在那个地方调用一个javascript方法(自己写的),看看是不是走完调用的方法后再走主方法。
------解决方案--------------------
肯定啊
你2个
if(){};
if(){};
if(){};
这个肯定都要执行
if(){}
else if(){}
else if(){}
这个肯定只执行一个
------解决方案--------------------
LZ不用这样写了,你那样写执行效率很低的!客户看见你给的提示会崩溃的!
既然JS做判断,就要按顺序判断,一个不符合,就return ,不要连着写if else if ....
------解决方案--------------------
验证一般分为逐个验证和整体验证的、、、不带你这样写验证的、亲