2013-09-21 46 views
0

我在javascript中創建了一個html驗證頁面。 以下是驗證碼。驗證表單在註冊過程中不起作用

SignUp.html

< 腳本類型= 「文/ JavaScript的」>

function validateForm() { 
var id = document.signup.LoginId; 
var password = document.signup.LoginPassword; 
var cpassword = document.signup.ConfirmPassword; 
var city = document.signup.City; 
var state = document.signup.State; 
var number = document.signup.PhoneNumber; 
var email = document.signup.Email; 
var address = document.signup.Address; 
var zipcode = document.signup.ZipCode; 

if(id.value == "") { 
    window.alert("Error: Username should be filled out."); 
    id.focus(); 
    return false; 
} 
re = /^\w+$/; 
if(!re.test(id.value)) { 
    window.alert ("Error: Username must contain only letters, numbers and underscores."); 
    id.focus(); 
    return false; 
} 

if(id.length < 6) { 
    window.alert("Error: Username must contain at least 6 charecters."); 
    id.focus(); 
    return false; 
} 
if(id.length > 12) { 
    window.alert("Error: Username must not be greater than 12 charecters."); 
    id.focus(); 
    return false; 
} 

if(password.value != "" && password.value == cpasswotd.value) { 
    if(password.length < 6) { 
     window.alert("Error: Password must contain at least 6 charecters."); 
     password.focus(); 
     return false; 
    } 
    if(password.length > 12) { 
     window.alert("Error: Password must not be greater than 12 charecters."); 
     password.focus(); 
     return false; 
    } 
    if(password.value == id.value) { 
     window.alert("Error: Password must be different from UserName."); 
     password.focus(); 
     return false; 
    } 
    re = /[0-9]/; 
    if(!re.test(password.value)) { 
     window.alert("Error: Password must contain at least one number."); 
     password.focus(); 
     return false; 
    } 
    re = /[a-z]/; 
    if(!re.test(password.value)) { 
     window.alert("Error: Password must contain at least one lowercase letter (a-z)."); 
     password.focus(); 
     return false; 
    } 
    re = /[A-Z]/; 
    if(!re.test(password.value)) { 
     window.alert("Error: Password must contain at least one uppercase letters (A-Z)."); 
     password.focus(); 
     return false; 
    } 
}else { 
    window.alert("Error: Please check that you've entered and Confirmed your Password."); 
    password.focus(); 
    return false; 
} 
window.alert("You have entered a valid password: "+password.value); 
return true; 



if(city.value == "") { 
    window.alert("City must not be null."); 
    city.focus(); 
    return false; 
} 

if(state.value == "") { 
    window.alert("State must not be null."); 
    state.focus(); 
    return false; 
} 

if(number.value == "") { 
    window.alert("Phone number must not be null."); 
    number.focus(); 
    return false; 
} 

if(number.length != 10) { 
    window.alert("Phone number must be 10 digits."); 
    number.focus(); 
    return false; 
} 

if (email.value == "") 
{ 
    window.alert("Please enter a valid e-mail address."); 
    email.focus(); 
    return false; 
} 

if (email.value.indexOf("@", 0) < 0) 
{ 
    window.alert("Please enter a valid e-mail address."); 
    email.focus(); 
    return false; 
} 

if (email.value.indexOf(".", 0) < 0) 
{ 
    window.alert("Please enter a valid e-mail address."); 
    email.focus(); 
    return false; 
} 

re = /^\w+$/ 
if(!re.test(address.value)) { 
    window.alert("Error: Address must contain only letters, numbers and underscores."); 
    address.focus(); 
    return false; 
} 

if(zipcode.value == "") { 
    window.alert("Error: Zipcode must not be null."); 
    zipcode.focus(); 
    return false; 
} 

if(zipcode.length > 6) { 
    window.alert("Error: zipcode must not less than 6 digits."); 
    zipcode.focus(); 
    return false; 
} 

} < /腳本>

當我嘗試輸入錯誤的電子郵件地址如「sapan @ 2」或「sapan」,它不會顯示任何錯誤信息,它會保存到數據庫中。

任何人都可以幫我解決這個問題。

謝謝。

+0

你可以發佈整個函數聲明嗎?這會幫助我幫助你。 :) –

+0

對不起,我無法完全發佈問題。我無法插入HTML代碼。 :-(你能告訴我如何發佈html代碼,這對我有幫助,謝謝 – sapan

+0

只需javascript代碼就可以了,如果你不能發佈整個函數,那麼我將無法提供幫助:) –

回答

0

我創建了一個簡單的測試註冊表單,並附帶了jQuery驗證處理程序。您可以根據需要修改此測試處理程序。

將下面的代碼在SignUp.html:

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <script type="text/javascript" src="jquery.min.js"></script> 
    </head> 
    <body> 
    <form id="myForm" name="myForm" action="" method="post"> 
     Username: <input type="text" id="username" value="" /> 
     <input type="submit" name="submit" /> 
    </form> 

    <script type="text/javascript"> 

     $("#myForm").submit(function(event) { 

      if ($("input#username").val() != "") { 
      alert('Logging you in!'); 
      return; 
      } 

      alert("Please enter the username!"); 
      event.preventDefault();   
     }); 

    </script> 
    </body> 
</html> 

對於這個工作,你需要把jquery.min.js在同一文件夾中的HTML文件。

0

有幾件事情可能是錯誤的。首先,如果您從'type =「submit」'的輸入'控件調用'validateForm'函數,則將其更改爲'type =「button」'。

其次,在你的「validateForm」函數的結尾添加以下行...

document.getElementById("yourformidhere").submit(); 

這將從單擊該按鈕時停止提交表單,只會提交表單,畢竟元素值已經通過驗證,這是你實際上想要完成的。

+0

對不起,它沒有解決。 – sapan

+0

請詳細說明。這應該工作,除非你遇到不同的錯誤。 –

+0

修改代碼後,我沒有輸入甚至是用戶名,那麼它沒有顯示任何錯誤,指出「用戶名不應該爲空」。什麼都沒發生。它沒有顯示任何錯誤。 – sapan