2012-06-16 132 views
0

當我點擊提交按鈕時,它顯示驗證權限,但在警報消息之後,頁面正在刷新,並且我鬆開了來自域的所有其他數據:S,我如何使它仍然保留其他領域填補。Javascript顯示警報,不刷新頁面

我試圖刪除提交事件後的window.location.reload()被調用,但仍然不工作:S。任何人有任何建議?

這是代碼:

function formValidation() { 
    var uid = document.registration.userid; 
    var passid = document.registration.passid; 
    var uname = document.registration.username; 
    var uadd = document.registration.address; 
    var ucountry = document.registration.country; 
    var uzip = document.registration.zip; 
    var uemail = document.registration.email; 
    var umsex = document.registration.msex; 
    var ufsex = document.registration.fsex; 
    if (userid_validation(uid, 5, 12)) { 
    if (passid_validation(passid, 7, 12)) { 
     if (allLetter(uname)) { 
     if (alphanumeric(uadd)) { 
      if (countryselect(ucountry)) { 
      if (allnumeric(uzip)) { 
       if (ValidateEmail(uemail)) { 
       if (validsex(umsex, ufsex)) {} 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
    return false; 

} 
function userid_validation(uid, mx, my) { 
    var uid_len = uid.value.length; 
    if (uid_len == 0 || uid_len >= my || uid_len < mx) { 
    alert("User Id should not be empty/length be between " + mx + " to " + my); 
    uid.focus(); 
    return false; 
    } 
    return true; 
} 

function passid_validation(passid, mx, my) { 
    var passid_len = passid.value.length; 
    if (passid_len == 0 || passid_len >= my || passid_len < mx) { 
    alert("Password should not be empty/length be between " + mx + " to " + my); 
    passid.focus(); 
    return false; 
    } 
    return true; 
} 

function allLetter(uname) { 
    var letters = /^[A-Za-z]+$/; 
    if (uname.value.match(letters)) { 
    return true; 
    } else { 
    alert('Username must have alphabet characters only'); 
    uname.focus(); 
    return false; 
    } 
} 

function alphanumeric(uadd) { 
    var letters = /^[0-9a-zA-Z]+$/; 
    if (uadd.value.match(letters)) { 
    return true; 
    } else { 
    alert('User address must have alphanumeric characters only'); 
    uadd.focus(); 
    return false; 
    } 
} 

function countryselect(ucountry) { 
    if (ucountry.value == "Default") { 
    alert('Select your country from the list'); 
    ucountry.focus(); 
    return false; 
    } else { 
    return true; 
    } 
} 

function allnumeric(uzip) { 
    var numbers = /^[0-9]+$/; 
    if (uzip.value.match(numbers)) { 
    return true; 
    } else { 
    alert('ZIP code must have numeric characters only'); 
    uzip.focus(); 
    return false; 
    } 
} 

function ValidateEmail(uemail) { 
    var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; 
    if (uemail.value.match(mailformat)) { 
    return true; 
    } else { 
    alert("You have entered an invalid email address!"); 
    uemail.focus(); 
    return false; 
    } 
} 
function validsex(umsex, ufsex) { 
    x = 0; 

    if (umsex.checked) { 
    x++; 
    } 
    if (ufsex.checked) { 
    x++; 
    } 
    if (x == 0) { 
    alert('Select Male/Female'); 
    umsex.focus(); 
    return false; 
    } else { 
    alert('Form Succesfully Submitted'); 
    window.location.reload() 
    return true; 
    } 
} 
+2

縮進一樣,一直是糟糕的代碼 –

+0

@ m.edmondson jsbeautifier救援:-) – Pointy

+3

@lulzim你應該表明驗證函數實際上是如何稱爲一個標誌。是的,如果你不希望頁面重新加載,你不應該調用'window.location.reload()'。 – Pointy

回答

1

兩個問題...

  1. 缺少返回

    的onsubmit = 「返回formValidation();」

  2. 缺失返回true;

    if(validsex(umsex,ufsex)){return true; }

+0

非常感謝 – lulzim