2012-06-02 93 views
2

當新用戶在我的網站註冊時,他們必須填寫一些表格。如果他們的密碼太短,不匹配,他們沒有填寫所有的字段或任何其他錯誤,我想提醒他們。目前它沒有提醒他們,所以我轉而確認。這也沒有工作,所以我考慮使用showModalDiaglog。這也不起作用,我脫離了想法。目前,我有這樣的:JavaScript警報/確認錯誤

HTML

<form> 
    First Name:<br/> <input id="firstname"/><br/> 
    Last Name: <br/> <input id="lastname"/><br/> 
    Age:  <br/> <input id="age"/><br/> 
    Your Email:<br/> <input id="email"/><br/> 
    Username: <br/> <input id="username"/><br/> 
    Password: <br/> <input id="password"/><br/> 
    Confirm Password:<br/> <input type="password" id="passwordconfirm"/><br/> 
    <br/> 
    <button size=20 onClick='getRegistrationFields()'>Submit</button> 
    </form>​ 

的JavaScript

function getRegistrationFields() { 
var first = document.getElementById("firstname").value; 
var last = document.getElementById("lastname").value; 
var email = document.getElementById("email").value; 
var age = document.getElementById("age").value; 
var username = document.getElementById("username").value; 
var password = document.getElemetnById("password").value; 
var confirm = document.getElementById("passwordconfirm").value; 
var empty = ""; 

if ((first === empty) || (last === empty) || (email === empty) || (age === empty) 
    || (username === empty) || (password === empty) || (confirm === empty)) { 
    var message = "Not all of the fields are filled out"; 
    prompt(message); 
    //showModalWindow("fields"); 
    return false; 
    } 

else { 
    age = parseInt(age); 

    if (password.length < 10) { 
     var message2 = "Password must be at least 10 characters long"; 
     prompt(message2); 
     //showModalWindow("passwordlength"); 
     return false; 
     } 

    else if (password != confirm) { 
     var message3 = "Passwords Do not match"; 
     prompt(message3); 
     //showModalWindow("passwordmatch"); 
     return false; 
     } 

    else if (age < 18) { 
     var message4 = "You must be older to register for this software."; 
     prompt(message4); 
     //showModalWindow("young"); 
     return false; 
     } 

    else { 
     var message5 = "All of the fields are correct. We will be processing your request"; 
     prompt(message5); 
     //showModalWindow("success"); 
     return true; 
     } 
    } 
} 

function showModalWindow(fileName) { 
    window.showModalDialog("alerts/" + url + ".html", "", "resizable: no; height: 150; width: 350;"); 
} 
+0

@mgraph你的意思是什麼exatley? –

+0

只是一個說明,你的HTML中的'password'是一個正常的文本框....可能要更改 –

+0

@OutlawLemur啊是的,謝謝 –

回答

4

嘗試

HTML:

<button size="20" id="btn_submit">Submit</button> 

JS:

var btn_submit = document.getElementById("btn_submit"); 
btn_submit.onclick = getRegistrationFields; 
+0

你正在爲'onclick'分配一個函數調用的返回值。除非被調用的函數返回一個函數,否則這是沒有意義的。刪除'()'。 – ThiefMaster

+0

謝謝!!!!!!!!!!有足夠的代表時會upvote! –

+0

@DanFlosh很高興我幫你:) – mgraph

1

有一個錯字:

var password = document.getElemetnById("password").value; 

你寫getElemetnById而不是getElementById

1

很容易讓你自己。

<input type="password" class="password" /> 
    <input type="text" class="age" /> 
    <script type="text/javascript"> 
     !(function() { 
      var txBxs = document.querySelectorAll('input[type=textbox]'), 
       i = txBxs.length; 
      while (i--) { 
       tkBxs[i].onchange = textBoxCheck; 
      }; 
     }()); 
     // 
     function textBoxCheck() { 
      // 
      switch (this.class) { 
       // 
       case 'password': 
        if (this.value.length < 10) 
        { alert('Password to short'); }; 
        break; 
        // 

       case 'age': 
        if (+this.value < 18) 
        { alert('To young kiddo!'); }; 
        break; 
      }; 
     }; 
    </script> 
+1

哈哈+1「太年輕的孩子!」 –

+2

雖然你的'if'機構可怕的縮進風格。 – ThiefMaster